实例
import requests
import parsel
import time
import os
start = time.time() # 记录程序开始时间
"""
1,定义url,伪造headers
2,请求数据
3,解析数据
4,提取数据
5,持久化保存
"""
# 判断文件是否存在
if not os.path.exists("./image"):
# 创建文件
os.mkdir("./image")
print("创建文件image成功")
# 1,定义url,伪造headers
url = "https://fm.qq.com/category/39087_38979"
headers = {
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36"
}
# 2,请求数据
response = requests.get(url=url,headers=headers).text
# 3,解析数据
sel = parsel.Selector(response)
lis = sel.css('.album-list .item')[0:-1]
count = 0
for li in lis:
time.sleep(0.5)
# 二次解析
img_url = li.css("img::attr(src)").get() # 图片url
title = li.css(".title>a::text").get() # 标题
img_response = requests.get(url=img_url,headers=headers).content # 再次请求图片
path = "./image/" + title + ".png" # 定义保存路径跟文件名
try: # 异常捕获
with open(path, "wb") as f: # 保存图片
f.write(img_response)
count += 1 # 累计爬取图片次数
print("保存成功:{0},第{1}张".format(title, count))
except Exception as e:
print(e)
end = time.time() # 记录程序结束时间
print("爬取完毕,一共{0}张图片,一共用时{0}秒".format(count, end - start / 1000))
图片预览
打卡第60天,对python大数据感兴趣的朋友欢迎一起讨论、交流,请多指教!