這篇文章主要介紹了python實(shí)現(xiàn)從網(wǎng)絡(luò)下載文件并獲得文件大小及類型的方法,涉及Python操作網(wǎng)絡(luò)文件的相關(guān)技巧,需要的朋友可以參考下
本文實(shí)例講述了python實(shí)現(xiàn)從網(wǎng)絡(luò)下載文件并獲得文件大小及類型的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
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())
希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。
更多信息請(qǐng)查看IT技術(shù)專欄