gpt4 book ai didi

python - 在python中下载文件

转载 作者:行者123 更新时间:2023-11-28 19:32:16 25 4
gpt4 key购买 nike

当我想使用此代码在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com