作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我想使用此代码在 python 中下载 jpeg 时:
def download(url, dest):
s = urllib2.urlopen(url)
content = s.read()
s.close()
d = open(dest,'w')
d.write(content)
d.close()
硬盘上的文件不可读,但是当我在 mozilla 中打开 jpeg 时它没问题,我正在使用 Windows 和 python 2.6一些解决方案?谢谢
最佳答案
您正在以文本模式打开文件并损坏它。 Python 将某些字节序列解释为 EOL 字符,并将它们作为该操作系统的适当 EOL 写出。您需要告诉 Python 以二进制模式打开目标文件。
将 d = open(dest,'w')
更改为 d = open(dest,'wb')
一切正常。
关于python - 在python中下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5186389/
我是一名优秀的程序员,十分优秀!