使用 PycURL 下载图像让我崩溃
我正在与 pycurl 作斗争。 这些是我的标题:
headers.append('User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:5.0) Gecko/20100101 Firefox/5.0')
headers.append('Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8')
headers.append('Accept-Language: de-de,de;q=0.8,en-us;q=0.5,en;q=0.3')
headers.append('Accept-Encoding: gzip, deflate')
headers.append('Accept-Charset: UTF-8,*')
headers.append('Connection: Keep-Alive')
原始:
我的内容获取:
正如你所看到的,我用 pycurl 得到的那个已经坏了。如果我将它们与文本比较工具进行比较,它会告诉我它们是相同的。 (行尾存在差异,原始文件只有 LF,损坏的文件有 CRLF,但我更改了这一点,现在我有相同的图像,但仍然损坏)
我从中下载的主机不是原因。我尝试从保管箱和本地 apache 执行相同的操作。两者都不起作用。
这就是我保存图像的方式:
self.buffer = StringIO.StringIO()
# other curl options like ssl, cookies, followlocation and GET Request URL Setup to the Image: http://dl.dropbox.com/u/25733986/test.jpg
self.curl.setopt(pycurl.WRITEFUNCTION, self.buffer.write)
# -> curl.perform()
f = open("temp/resources/%s" % (filename,), 'w')
f.write(self.buffer.getvalue())
f.close()
如果有人对此有一些建议,我会很高兴,这样我就可以找到我的错误。
I'm struggling with pycurl.
These are my headers:
headers.append('User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:5.0) Gecko/20100101 Firefox/5.0')
headers.append('Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8')
headers.append('Accept-Language: de-de,de;q=0.8,en-us;q=0.5,en;q=0.3')
headers.append('Accept-Encoding: gzip, deflate')
headers.append('Accept-Charset: UTF-8,*')
headers.append('Connection: Keep-Alive')
Original:
What I get:
As you can see, the one I get with pycurl is broken. If I compare them with a text-compare-tool, it tells me they are the same. (There was a difference in line-endings where the original had only LF and the broken had CRLF, but I changed that and now I have identical images, still broken)
The host where I'm downloading from, is not the reason. I tried to do the same from the dropbox and a local apache. Both didn't work.
This is how I save the image:
self.buffer = StringIO.StringIO()
# other curl options like ssl, cookies, followlocation and GET Request URL Setup to the Image: http://dl.dropbox.com/u/25733986/test.jpg
self.curl.setopt(pycurl.WRITEFUNCTION, self.buffer.write)
# -> curl.perform()
f = open("temp/resources/%s" % (filename,), 'w')
f.write(self.buffer.getvalue())
f.close()
I would be happy if anyone has some suggestions on this, so I can find my error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,现在我终于通过发布这个问题来思考这个问题,我得到了解决方案。
我需要以二进制模式打开该文件。
我希望这个问题可以在某个时候、无论如何帮助别人。
感谢 stackoverflow 让我思考这个问题。 :)
Okay, now where I have finally meditated over this by posting this question I got the solution.
I needed to open the file in binary mode.
I hope this Question helps someone else, sometime, anyhow.
Thanks stackoverflow for making me meditate on this. :)