作者ms0561188 (蟑螂吃滷蛋)
看板Python
標題[問題] 圖片爬蟲無法抓取
時間Mon Jun 12 21:31:35 2017
各位前輩大家好,最近無聊玩玩爬蟲,有看到可以將網路圖片抓取下來的方法
以下是我主要的程式碼
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)
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.123.92.136
※ 文章網址: https://www.ptt.cc/bbs/Python/M.1497274299.A.774.html
→ zerof: 你前面先 print 掉了啊.... 06/12 21:43
→ zerof: 直接 f.write(response.content) 就好了 06/12 21:44
→ ms0561188: 已解決 感謝樓上的大大!!!! 06/12 21:49
推 coeric: response = requests.get( ...... ,stream=True,....) 06/12 21:53
→ coeric: 多這一行即可 06/12 21:53
→ coeric: 是個黑人....XDDDD 06/12 21:57
→ ms0561188: 謝謝樓上的大大,可以用了 謝謝 06/12 21:58
→ coeric: 因為........我也常幹這種事....忘了加這一段 06/12 21:59