|
| 1 | +import requests |
| 2 | +import pathlib |
| 3 | +import shutil |
| 4 | +import json |
| 5 | + |
| 6 | +base_url = 'https://api.bilibili.com/x/space/arc/search?mid=320491072&ps=30&tid=0&pn={0}&keyword=&order=pubdate&jsonp=jsonp' |
| 7 | + |
| 8 | +headers = { |
| 9 | + "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36" |
| 10 | +} |
| 11 | + |
| 12 | +def get_total_page(): |
| 13 | + response = requests.get(base_url.format(1), headers=headers) |
| 14 | + j = response.content |
| 15 | + data = json.loads(j) |
| 16 | + page = data['data']['page'] |
| 17 | + return int(page['count'] / page['ps']) + 1 |
| 18 | + |
| 19 | + |
| 20 | +def get_image_urls(): |
| 21 | + total_page = get_total_page()+1 |
| 22 | + |
| 23 | + for i in range(1, total_page): |
| 24 | + url = base_url.format(i) |
| 25 | + response = requests.get(url, headers=headers) |
| 26 | + j = response.content |
| 27 | + data = json.loads(j) |
| 28 | + for i in data['data']['list']['vlist']: |
| 29 | + yield {'name': i['title'], 'url': i["pic"]} |
| 30 | + |
| 31 | + |
| 32 | +def remove_unvalid_chars(s): |
| 33 | + for c in r'''"'<>/\|:*?''': |
| 34 | + s = s.replace(c, '') |
| 35 | + return s |
| 36 | + |
| 37 | + |
| 38 | +def download_images(): |
| 39 | + save_folder = r'~/Desktop/images' |
| 40 | + folder = pathlib.Path(save_folder).expanduser() |
| 41 | + if not folder.exists(): |
| 42 | + folder.mkdir() |
| 43 | + for i in get_image_urls(): |
| 44 | + response = requests.get(i['url'], stream=True) |
| 45 | + filename = remove_unvalid_chars(i["name"])+'.jpg' |
| 46 | + with open(folder/filename, 'wb') as f: |
| 47 | + shutil.copyfileobj(response.raw, f) |
| 48 | + print(f'{i["name"]}.jpg下载完成') |
| 49 | + |
| 50 | + |
| 51 | +download_images() |
0 commit comments