访问url下载文件----python


工作上有时候有需求,会下载pdf,doc,zip等文件,可以用以下方法,推荐使用第一种

下载文件:

import urllib 
import urllib2 
import requests   
url = 'http://www.blog.pythonlibrary.org/wp-content/uploads/2012/06/wxDbViewer.zip'  

# 第一种 urlretrive 第一种用得比较多 print "downloading with urllib" urllib.urlretrieve(url, "code.zip")
# 第二种 urlopen 然后写入文件
print "downloading with urllib2" f = urllib2.urlopen(url) data = f.read() with open("code2.zip", "wb") as code: code.write(data)

# 第三种 requests.get 然后写入文件
print "downloading with requests" r = requests.get(url) with open("code3.zip", "wb") as code: code.write(r.content)

参考链接: http://outofmemory.cn/code-snippet/83/sanzhong-Python-xiazai-url-save-file-code


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM