Python 爬取必應bing桌布
阿新 • • 發佈:2020-11-19
前言
喜歡BING的高清大圖,快節奏的生活增添一絲絲溫暖
以後是python完整程式碼(可以選擇下載的時間範圍)
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('Z:\\必應桌布'): 21 pass 22 else: 23 os.mkdir('Z:\\必應桌布') 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"Z:\\必應桌布\\{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()