这是我尝试过的代码。文件为 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 文件。
谢谢你的帮助!
2014 年 3 月 10 日编辑//这是 spynner 给我的调试消息。请注意 php 提供的图像的正确格式的 url,以及正确下载的 .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)