python爬蟲學習(一)爬取高清桌布(各種主流大小的高清圖)
阿新 • • 發佈:2021-06-24
from bs4 import BeautifulSoup import requests import time import os def get_photo(key): url = "https://desk.zol.com.cn/meinv/"+str(key)+".html" resp = requests.get(url) resp.encoding = "gb2312" main_page = BeautifulSoup(resp.text,"html.parser") ul = main_page.find("ul",class_="pic-list2") a = ul.find_all("a",class_="pic") for i in a: url_detail = "https://desk.zol.com.cn/"+str(i.get('href').strip("/")) child_resp = requests.get(url_detail,"html.parser") child_page = BeautifulSoup(child_resp.text,"html.parser") child_dd = child_page.find("dd",id="tagfbl") child_a = child_dd.find_all("a",class_="") num = 0 for i in child_a: child_url = "https://desk.zol.com.cn"+str(i.get('href')) child_name = child_url.strip('https://desk.zol.com.cn/showpic/')+str('.jpg') child_html = requests.get(child_url) child_img= BeautifulSoup(child_html.text,"html.parser").find("img").get('src') child_requests = requests.get(child_img) if (num == 0): if not os.path.exists('爬取高清圖片'): os.mkdir('爬取高清圖片') with open("D:/code/python/爬取高清圖片/"+child_name,mode="wb") as f: f.write(child_requests.content) print("sucessful!!!" +child_name) num+=1 time.sleep(1) def start(): for i in range(20): get_photo(i+1)if __name__ == "__main__": start()