import requests
from bs4 import BeautifulSoup
url = "https://bizhi.ijinshan.com/2/300657.shtml"
headers={
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36 Edg/91.0.864.41"
}
resp = requests.get(url=url,headers=headers)
resp.encoding = "utf-8"#处理乱码
#print(resp.text)
#把源代码交给bs4
page = BeautifulSoup(resp.text,"html.parser")
alist = page.find("section",class_="wallpaper-wrapper").find_all("a")
#print(alist)
for a in alist:
herf=a.get('href')
#拿到子页的源代码
cresp=requests.get(herf)
cresp.encoding = "utf-8"
#拿到子页面的下载链接
cpage=BeautifulSoup(cresp.text,"html.parser")
p=cpage.find("div",class_="content-detail-wallpaper")
img=p.find("img")
src=img.get("src")
#下载图片
img_resp=requests.get(src)
#img_resp.content#拿到字节
img_name=src.split("/")[-1]#拿到最后一个/后的内容作为名字
with open("img/"+img_name,mode="wb") as f:
f.write(img_resp.content)#把图片写入文件
print("over",img_name)
print("allove")
resp.close()
cresp.close()
|