我正在使用Python urllib2下载文件。如何检查文件大小有多大? [英] I am downloading a file using Python urllib2. How do I check how large the file size is?

查看:443
本文介绍了我正在使用Python urllib2下载文件。如何检查文件大小有多大?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果它是大的...然后停止下载?
我不想下载大于12MB的文件。

And if it is large...then stop the download? I don't want to download files that are larger than 12MB.

request = urllib2.Request(ep_url)
request.add_header('User-Agent',random.choice(agents))
thefile = urllib2.urlopen(request).read()


推荐答案

没有必要 bobince 完成并下载到httplib。你可以直接用urllib做这些:

There's no need as bobince did and drop to httplib. You can do all that with urllib directly:

>>> import urllib2
>>> f = urllib2.urlopen("http://dalkescientific.com")
>>> f.headers.items()
[('content-length', '7535'), ('accept-ranges', 'bytes'), ('server', 'Apache/2.2.14'),
 ('last-modified', 'Sun, 09 Mar 2008 00:27:43 GMT'), ('connection', 'close'),
 ('etag', '"19fa87-1d6f-447f627da7dc0"'), ('date', 'Wed, 28 Oct 2009 19:59:10 GMT'),
 ('content-type', 'text/html')]
>>> f.headers["Content-Length"]
'7535'
>>> 

如果您使用httplib,那么您可能需要实施重定向处理,代理支持和其他好东西urllib2为你做。

If you use httplib then you may have to implement redirect handling, proxy support, and the other nice things that urllib2 does for you.

这篇关于我正在使用Python urllib2下载文件。如何检查文件大小有多大?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆