爬取图片再次求助
import reimport requests
if __name__ == '__main__':
url='https://www.qiushibaike.com/imgrank/'
headers={
'User-Agent':'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Mobile Safari/537.36'
}
page_content=requests.get(url=url,headers=headers).text
regex='<img src="(//pic.qiushibaike.com/system/.*?)"'
image_src_list=re.findall(regex,page_content,re.S)
print(image_src_list)
for i in image_src_list:
i='https:'+i
image_data=requests.get(url=url,headers=headers).content
image_name=i.split('/')[-1]
image_path=r'C:\Users\liu\Desktop\1'+image_name
with open(image_path,'wb') as fp:
fp.write(image_data)
print(image_name,'完成')
为什么下载的图片打不开呢
for 循环访问链接错了,访问的是 i 图片链接
image_data=requests.get(url=url,headers=headers).content
改成:
image_data=requests.get(url=i,headers=headers).content
第 15 行
image_data=requests.get(url=i,headers=headers).content
页:
[1]