简体   繁体   English

使用python进行不完整的文件下载

[英]Incomplete filedownloads with python

I have the following python code: 我有以下python代码:

import urllib2
DIR = '/home/aaron/Desktop/aaron-file/media/'
LOC = DIR+'/'+bounty.title+'.mp3'

u = urllib2.urlopen(url, 'rb')
localFile = open(LOC, 'wb')
localFile.write(u.read())
localFile.close()
u.close()

It only creates small (roughly 60kb files), the files operate correctly accept for the sudden stop. 它只会创建较小的文件(大约60kb),文件运行正常,可以突然停止。 When I download from firefox (copy and paste the same url) I get full sized files (roughly 2mb). 当我从firefox下载(复制并粘贴相同的URL)时,我得到了完整尺寸的文件(大约2mb)。

I am running 32bit ubuntu. 我正在运行32位ubuntu。

UPDATE: I believe it may be a problem with the http content-length being inaccurate. 更新:我相信这可能与http内容长度不正确有关。 How then would I ignore/set different length. 那我将如何忽略/设置不同的长度。

Thanks. 谢谢。

Maybe the server, from which you are downloading rejects urllib2 default user agent. 也许您要从中下载的服务器拒绝urllib2默认用户代理。 Consider constructing a custom opener with a fake user agent header, like this: 考虑使用伪造的用户代理标头构造一个自定义打开器,如下所示:

opener = urllib2.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]

and use opener() instead of urllib2.urlopen() 并使用opener()代替urllib2.urlopen()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM