python下载文件的几种常用方法

python笔记1,121字数 506阅读1分41秒阅读模式

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)
withopen("code3.zip","wb") as code:
    code.write(r.content)

具体详情可参见:http://www.blog.pythonlibrary.org/2012/06/07/python-101-how-to-download-a-file/

 最后更新:2016-10-19
  • 本文由 asdfasd 发表于 2016-10-1814:34:30
  • 转载请务必保留本文链接:http://wp.fangfa.me/python-note/python-download-file.html