1 import re
2 import os
3 import requests
4 from time import sleep
5
6 headers = {
7 "User-Agent": ("Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:64.0) "
8 "Gecko/20100101 Firefox/64.0")
9 }
10
11 def get_index(resolution, index=1):
12 url = f"https://bing.ioliu.cn/ranking?p={index}"
13 res = requests.get(url, headers=headers)
14 urls = re.findall('pic=(.*?)\\.jpg', res.text)
15 _old_resolution = urls[1].split("_")[-1]
16 return {url.split("/")[-1].replace(_old_resolution, resolution): url.replace(_old_resolution, resolution) + ".jpg"
17 for url in urls}
18
19 def download_pic(pics):
20 if os.path.exists('必应壁纸'):
21 pass
22 else:
23 os.mkdir('必应壁纸')
24 print('目录创建成功')
25 try:
26 for pic_name, pic_url in pics.items():
27 res = requests.get(pic_url, headers=headers)
28 with open(f"必应壁纸\\{pic_name}.jpg", mode="wb") as f:
29 f.write(res.content)
30 print(f"{pic_name} 下载完成")
31 except Exception as e:
32 print("下载出错", e)
33
34 def input_index():
35 print("必应壁纸下载工具, 本工具未经资源站授权.")
36 print("仅做学习和交流之用, 随时有可能停止维护.")
37 print("目前资源站收容页数为87,当前仅提供1920x1080分辨率下载")
38 while True:
39 sleep(0.1)
40 index = input("请输入要下载的页数(Max=87):")
41 try:
42 if index == "Q":
43 exit()
44 index = 87 if int(index) > 87 else int(index)
45 return index
46 except ValueError:
47 print("请输入数字, 或输入Q退出!")
48
49 def main():
50 index = input_index()
51 i = 1
52 while i <= index:
53 print(f"当前第{i}页,共需要下载{index}页")
54 pics = get_index("1920x1080", i)
55 download_pic(pics)
56 i += 1
57 print("下载完成,将在3秒后关闭...")
58 sleep(1)
59 print("2")
60 sleep(1)
61 print("1")
62 sleep(1)
63 print("0")
64
65 if __name__ == '__main__':
66 main()