python下载文件常用方法示例

发布时间:2020-12-29编辑:脚本学堂
有关python下载文件的几种方法,python实现文件的下载离不开urllib,urllib2,requests等模块,这里分享一个例子,需要的朋友参考下。

python中下载文件常用模块有urllib,urllib2,requests。

例子:
 

复制代码 代码示例:
# Python 2 code
import urllib
import urllib2
import requests
              
url = 'http://192.168.1.100/test.zip'
              
print "downloading with urllib"
urllib.urlretrieve(url, "code.zip")
              
print "downloading with urllib2"
f = urllib2.urlopen(url)
data = f.read()
with open("code2.zip", "wb") as code:
    code.write(data)
              
print "downloading with requests"
r = requests.get(url)
with open("code3.zip", "wb") as code:
    code.write(r.content)