脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - python实现从网络下载文件并获得文件大小及类型的方法

python实现从网络下载文件并获得文件大小及类型的方法

2020-06-15 09:41feiwen Python

这篇文章主要介绍了python实现从网络下载文件并获得文件大小及类型的方法,涉及Python操作网络文件的相关技巧,需要的朋友可以参考下

本文实例讲述了python实现从网络下载文件并获得文件大小及类型的方法。分享给大家供大家参考。具体实现方法如下:

?
1
2
3
4
5
6
7
8
9
10
11
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程序设计有所帮助。

延伸 · 阅读

精彩推荐