[英]Spynner downloads a zero-byte .jpg with browser.download(url, filename) - Python, PHP
這是我嘗試過的代碼。 文件為0字節。 我還設置了imagedata = br.download(...),它的len()報告為0。 我已經待了幾個小時了...有什么主意嗎?
pre_record_soup='[<img src='/show_pic.php?id=316600'>]' #simplified
def func_get_pic(pre_record_soup, br=spynner.Browser()):
baseurl='http://www.testsite.com/'
for record in pre_record_soup:
imagetag=record.find('img')
filename = 'image.jpg' #set name of file afterdownload
try:
if imagetag:
piclink = imagetag.find('img')['src']
else:
piclink = 'basicimages/icons/icon.gif'
filename = 'icon.gif'
except TypeError:
return None
print baseurl+piclink #this prints the expected link
print filename #this prints the filename I want
with open('/home/myhome/'+filename, 'wb') as handle:
br.download(baseurl+piclink,handle) #not retrieving image...
我還在spynner的經過身份驗證的會話中調用此函數。 所以spynner將我登錄到一個網站,然后我抓取了此數據和其他數據。 其他數據(文本)沒有問題。 此外,當我在瀏覽器中訪問圖像URL時,它會正確顯示jpeg文件。
謝謝你的幫助!
edit-10 March 2014 //這是spynner給我的調試消息。 請注意,該php提供的圖像的格式正確,並且在正確下載的.gif中缺少“從下載流中讀取”:
http://www.testsite.com/show_pic.php?id=81851
Request: GET http://www.testsite.com/show_pic.php?id=81851
Start download: http://www.testsite.com/show_pic.php?id=81851
Download finished: http://www.testsite.com/show_pic.php?id=81851
http://www.testsite.com/basicimages/icons/icon.gif
Request: GET http://www.testsite.com/basicimages/icons/icon.gif
Start download: http://www.testsite.com/basicimages/icons/icon.gif
Read from download stream (419 bytes): http://www.testsite.com/basicimages/icons/icon.gif
Download finished: http://www.testsite.com/basicimages/icons/icon.gif
br.load嘗試產生的其他信息調試流。 請注意,內容長度為0個字節。 這會在Firefox中加載FINE。
Page load started
Request: GET http://www.testsite.com/show_pic.php?id=81851
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.21 (KHTML, like Gecko) Qt/4.8.4 Safari/537.21
Reply: 200/OK - http://www.testsite.com/show_pic.php?id=81851
Date: Tue, 11 Mar 2014 01:16:35 GMT
Server: Apache
Set-Cookie: PHPSESSID=abvcv4j6hbu57a638tc8pg8i77b19bl0; path=/
Content-Length: 0
Connection: close
Content-Type: text/html
Page load finished (39 bytes): http://www.testsite.com/show_pic.php?id=81851 (successful)
根據您的代碼,解析后的piclink
具有:
http://www.testsite.com/show_pic.php?id=316600
現在您正在執行baseurl+piclink
,這意味着:
http://www.testsite.com/http://www.testsite.com/show_pic.php?id=316600
這樣您現在知道錯誤在哪里。 相應地調整URL,它將解決您的問題!
回答:
從登錄到testsite的同一代碼之外調用函數將打開其他瀏覽器。 復制並粘貼到登錄功能中的func_get_pic代碼可以正常工作。 這是解決方法,直到我弄清楚如何將登錄會話從一個功能傳遞到另一個功能。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.