[问题] 图片爬虫无法抓取

楼主: ms0561188 (蟑螂吃卤蛋)   2017-06-12 21:31:35
各位前辈大家好,最近无聊玩玩爬虫,有看到可以将网络图片抓取下来的方法
以下是我主要的程式码
header={
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36
(KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'
}
response = requests.get('http://img.ltn.com.tw/Upload/liveNews/BigPic/600_phpNlWIKt.jpg',headers=header)
print response
f = open('block.jpg', 'wb')
shutil.copyfileobj(response.raw,f)
f.close()
确实有response 200,但抓取下来的图片字节为0,想请教一下各位大大原因,谢谢。
(环境为Pycharm)
作者: zerof (猫橘毛发呆雕像)   2017-06-12 21:43:00
你前面先 print 掉了啊....直接 f.write(response.content) 就好了
楼主: ms0561188 (蟑螂吃卤蛋)   2017-06-12 21:49:00
已解决 感谢楼上的大大!!!!
作者: coeric ( )   2017-06-12 21:53:00
response = requests.get( ...... ,stream=True,....)多这一行即可是个黑人....XDDDD
楼主: ms0561188 (蟑螂吃卤蛋)   2017-06-12 21:58:00
谢谢楼上的大大,可以用了 谢谢
作者: coeric ( )   2017-06-12 21:59:00
因为........我也常干这种事....忘了加这一段

Links booklink

Contact Us: admin [ a t ] ucptt.com