learning python reptile pictures

import re
import os
import requests
from time import sleep

headers = {
"User-Agent": ("Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:64.0) "
"Gecko/20100101 Firefox/64.0")
}

def get_index(resolution, index=1):
url = f"https://bing.ioliu.cn/ranking?p={index}"
res = requests.get(url, headers=headers)
urls = re.findall('pic=(.*?)\\.jpg', res.text)
_old_resolution = urls[1].split("_")[-1]
return {url.split("/")[-1].replace(_old_resolution, resolution): url.replace(_old_resolution, resolution) + ".jpg"
for url in urls}

def download_pic(pics):
if os.path.exists ( 'Bing wallpaper'):
Pass
the else:
os.mkdir ( 'Bing wallpaper')
Print ( 'directory successfully created')
the try:
for pic_name, pic_url in pics.items ():
RES = requests.get (pic_url, headers = headers)
with Open (f " Bing wallpaper .jpg} pic_name {\\ ", MODE =" WB ") AS F:
f.write (res.content)
Print (F"} {pic_name download completed ")
the except Exception AS E:
Print (" Download error ", E)

DEF input_index ():
Print (" Bing wallpaper download tool, this tool without authorization resources station ").
Print (" make learning and communication purposes only, may at any time stop maintenance ").
Print (" resources currently receiving station is 87 pages, is currently available only 1920x1080 resolution download ")
the while True:
SLEEP (0.1)
index = the iNPUT (" Please enter the number of pages to be downloaded (Max = 87): ")
try:
if index == "Q":
Exit ()
index = 87 IF int (index)> 87 the else int (index)
return index
the except a ValueError:
Print ( "Please enter a number, or enter Q to exit!")

DEF main ():
index = input_index ()
I =. 1
the while I <= index:
Print (F "Present {i} page of the need to download {index} p")
pics = get_index ( "1920x1080", I)
download_pic (pics)
I + =. 1
Print ( "the download is complete, will be closed after 3 seconds ... ")
SLEEP (. 1)
Print (" 2 ")
SLEEP (. 1)
Print (". 1 ")
SLEEP (. 1)
Print (" 0 ")

IF the __name__ == '__main__':
main ()

Guess you like

Origin www.cnblogs.com/hfct/p/10978024.html