python - 小白提问:爬虫抓取并保存的图片无法打开
天蓬老师
天蓬老师 2017-04-18 09:03:03
0
3
1249

1.我写了个爬虫去爬取页面,成功的拿到了所有图片的网络地址
2.下载保存到本地后,我去打开图片,发现一只在转圈无法打开
]2]

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

répondre à tous(3)
小葫芦

已经解决,自己找到了方法
'''
with open(os.path.join(filename, image_name), 'wb') as fs:

        fs.write(r.content)
        fs.close

'''

洪涛

有可能触发了反爬虫机制或者 URL 不对,导致下载下来的实际是文本文件。
用文本编辑器打开看看内容?

伊谢尔伦

requests文档:

In general, however, you should use a pattern like this to save what is being streamed to a file:

with open(filename, 'wb') as fd:
    for chunk in r.iter_content(chunk_size):
        fd.write(chunk)

测试:

with open('./pic2/'+str(self.picnum)+'.jpeg', 'wb') as fd:
    for chunk in r.iter_content():
        fd.write(chunk)
print('第%s图片下载成功。' % self.picnum)
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!