使用 PycURL 下载图像让我崩溃

发布于 2024-11-14 12:54:45 字数 1273 浏览 4 评论 0原文

我正在与 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')

原始: http://dl.dropbox.com/u/25733986/test.jpg

我的内容获取: http://dl.dropbox.com/u/25733986/test_kaputt.jpg

正如你所看到的,我用 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: http://dl.dropbox.com/u/25733986/test.jpg

What I get: http://dl.dropbox.com/u/25733986/test_kaputt.jpg

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

轮廓§ 2024-11-21 12:54:45

好吧,现在我终于通过发布这个问题来思考这个问题,我得到了解决方案。

我需要以二进制模式打开该文件。

f = open("temp/resources/%s" % (filename,), 'wb')

我希望这个问题可以在某个时候、无论如何帮助别人。

感谢 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.

f = open("temp/resources/%s" % (filename,), 'wb')

I hope this Question helps someone else, sometime, anyhow.

Thanks stackoverflow for making me meditate on this. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文