BeautifulSoup爬取墙纸
import bs4
import requests
from bs4 import BeautifulSoup
from urllib.parse import urljoin
import time
url = 'https://desk.zol.com.cn/pc/'
headers = {
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.60 Safari/537.36'
}
resp = requests.get(url, headers=headers)
resp.encoding = 'gbk'
page_content = BeautifulSoup(resp.text,"html.parser")
li = page_content.find("ul",attrs={"class":"pic-list2"}).find_all("a")
for a in li:
href = a.get("href")
text = a.find("em").text
if href.endswith(".exe"):
continue
href = urljoin(url,href)
img_result = requests.get(href,headers=headers)
img_text = img_result.text
img_page_source = BeautifulSoup(img_text,"html.parser")
img_a = img_page_source.find("img",attrs={"id":"bigImg"})
img_src=img_a.get("src")
img_url = img_src.split("/")[-1]
img_resp = requests.get(img_src)
print(img_url)
with open(img_url,"w") as f:
f.write(img_resp.content)
time.sleep(1)