import requests
import re
url = ‘https://desk.zol.com.cn/fengjing/2560x1600/’
headers = {
‘user-agent’: ‘Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0’
}
resp = requests.get(url=url, headers=headers)
正则表达式用于匹配src属性里的jpg图片链接
re_list = re.compile(r’src=“(.*?.jpg)”')
查找所有匹配的图片链接
re_data = re_list.findall(resp.text)
#print(re_data)
index = 0
for u in re_data:
print(u)
header = {
'user - agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)AppleWebKit/537.36 (KHTML, like Gecko)Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0',
'Referer':'https: // desk.zol.com.cn / bizhi / 9991_119818_2.html'
}
# 针对每个图片链接单独发起请求获取图片的二进制数据
pic_resp = requests.get(u, headers = header)
print(pic_resp.status_code)#响应码,200成功,403拒绝,404丢失,500错误。
with open(f'0{index}.jpg', mode='wb') as f:
f.write(pic_resp.content)
print(f"0{index}.jpg下载完成!")
index += 1