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)
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/

  • 版权声明:除非注明,本博客均为北京SEO方法的原创文章,转载或引用请以超链接形式标明本文地址,否则会在SEO圈内公开此种不尊重版权的行为,谢谢合作!本文地址:https://seofangfa.com/python-note/python-download-file.html
    本文信息:于4个月前,由方法SEO发表,共 506字。

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: