Spynner使用browser.download(url,filename)下载零字节.jpg-Python,PHP

2024-10-01 15:47:10 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我试过的密码。文件为0字节。我还设置了imagedata=br.下载(…)并为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中缺少“从下载流读取”:

^{pr2}$

来自的附加信息调试流br.负荷尝试。请注意,内容长度为0字节。这在Firefox中很好的加载。。。啊!在

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)

Tags: brcomidhttpwwwshowfilenamerecord
2条回答

回答:

从登录到testsite的同一代码外部调用函数将打开另一个浏览器。复制并粘贴到登录函数中的func_get_pic代码工作正常。这是解决方法,直到我弄清楚如何将登录会话从一个函数传递到另一个函数。在

根据您的代码,解析后您的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

现在你知道错误在哪里了。调整相应的网址,它将解决你的问题!在

相关问题 更多 >

    热门问题