python爬取网站图片保存到本地文件夹
爬取的网站
https://wallpaperscraft.com/catalog/anime
爬取代码
# 导包
import os
import requests
import parsel
from parsel import Selector
def download_onepagephoto(website_url,count): # 下载一页图片
# 用i暂存传输过来的count值
i=count
# 发送请求
response = requests.get(website_url)
response.encoding = response.apparent_encoding
# 很关键的一步,构建Selector对象
sel = Selector(response.text)
# 获取到网页中样式为wallpapers__item类下a标签的href的值
index = sel.css('.wallpapers__item a::attr(href)').getall()
# 遍历进入每个图片
for line in index:
# 模拟进入另一个页面,如法炮制上述操作
response = requests.get("https://wallpaperscraft.com"+line)
response.encoding = response.apparent_encoding
sel = Selector(response.text)
index2 = sel.css('.wallpaper__placeholder a::attr(href)').getall()
if len(index2)!=0:
nameurl=index2[0]
# 获取到图片链接,将其保存到同级目录本地photo文件夹
photo=requests.get(nameurl).content
with open("photo/"+str(i)+".jpg","wb") as fp:
fp.write(photo)
print(str(i)+" already success")
i=i+1
return i
count=1
#爬取第一页
count=download_onepagephoto("https://wallpaperscraft.com/catalog/anime/1920x1080",count)
#爬取第二页及以后
for temp in range(2,174):
count=download_onepagephoto("https://wallpaperscraft.com/catalog/anime/1920x1080/page"+str(temp),count)
print("第"+str(temp)+"页图片爬取完成")
【创作不易,望点赞收藏,若有疑问,请评论,谢谢】