python3获取url文件大小示例代码
在python3中,urllib2被替换为urllib.requeset,因此头文件中添加
import urllib.request as urllib2 def getRemoteFileSize(url, proxy=None): """ 通过content-length头获取远程文件大小 url - 目标文件URL proxy - 代理 """ opener = urllib2.build_opener() if proxy: if url.lower().startswith('https://'): opener.add_handler(urllib2.ProxyHandler({'https' : proxy})) else: opener.add_handler(urllib2.ProxyHandler({'http' : proxy})) try: request = urllib2.Request(url) request.get_method = lambda: 'HEAD' response = opener.open(request) response.read() except Exception: return 0 else: print(response.headers) fileSize = dict(response.headers).get('content-length', 0) return int(fileSize)
使用上段代码发现输出为0,考虑应该是没查询到content-length字段,打印response.headers字段后,发现content-length字段应改为Content-Length,改后正常
def getRemoteFileSize(url, proxy=None): """ 通过content-length头获取远程文件大小 url - 目标文件URL proxy - 代理 """ opener = urllib2.build_opener() if proxy: if url.lower().startswith('https://'): opener.add_handler(urllib2.ProxyHandler({'https' : proxy})) else: opener.add_handler(urllib2.ProxyHandler({'http' : proxy})) try: request = urllib2.Request(url) request.get_method = lambda: 'HEAD' response = opener.open(request) response.read() except Exception: return 0 else: print(response.headers) fileSize = dict(response.headers).get('Content-Length', 0) return int(fileSize)
总结
以上所述是小编给大家介绍的python3获取url文件大小示例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我们网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
赞 (0)