python实现从网络下载文件并获得文件大小及类型的方法
时间:2021-03-10 09:23:55|栏目:Python代码|点击: 次
本文实例讲述了python实现从网络下载文件并获得文件大小及类型的方法。分享给大家供大家参考。具体实现方法如下:
import urllib2
from settings import COOKIES
opener = urllib2.build_opener()
cookies = ";".join("%s=%s" % (k, v) for k, v in COOKIES.items())
opener.addheaders.append(('Cookie', cookies))
req = opener.open(link)
meta = req.info()
file_size = int(meta.getheaders("Content-Length")[0])
content_type = meta.getheaders('Content-Type')[0].split(';')[0]
print file_size, content_type
open(file_name, 'wb').write(req.read())
希望本文所述对大家的Python程序设计有所帮助。
上一篇:PyCharm2020.1.1与Python3.7.7的安装教程图文详解
栏 目:Python代码
本文标题:python实现从网络下载文件并获得文件大小及类型的方法
本文地址:http://www.codeinn.net/misctech/77711.html






