使用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,但我已经改过来了,现在两张图片看起来是一样的,还是坏掉)
我下载的主机不是问题所在。我尝试过从Dropbox和本地的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()
如果有人能给我一些建议,让我找到错误,我会很高兴。
1 个回答
6
好的,现在我终于想明白了这个问题,发了这个问题后我找到了答案。
我需要以二进制模式打开文件。
f = open("temp/resources/%s" % (filename,), 'wb')
希望这个问题能在某个时候帮助到其他人。
感谢StackOverflow让我思考这个问题。:)