2014-06-18 119 views
0

我想從每次請求時隨機生成的網頁上保存圖片,我嘗試使用.retrieve()函數獲取它,但它不起作用(它下載另一個隨機圖片)。使用Python機械化保存隨機生成的圖片

如果有必要,我不介意下載整個頁面。

+1

顯示您的代碼。如果圖像始終具有相同的名稱/路徑,則可以使用'curl'來代替python。 – furas

回答

1
import urllib2 
referer = 'link of the page of image' 
image = 'image link ' 

req = urllib2.Request(image) 
req.add_header('Referer', referer) # here is the trick 
response = urllib2.urlopen(req) 

output = open('out.jpg','wb') 
output.write(response.read()) 
output.close() 

這是您的問題的解決方案。