python下载文件的几种常用方法 – 王伟 – 51CTO技术博客

其他杂项25字数 637阅读2分7秒阅读模式

python中下载文件常用的几个模块有urllib,urllib2,requests,方法也很简单,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 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"
= urllib2.urlopen(url)
data = f.read()
with open("code2.zip""wb") as code:
    code.write(data)
               
print "downloading with requests"
= requests.get(url)
with open("code3.zip""wb") as code:
    code.write(r.content)

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

 
  • 本文由 asdfasd 发表于 2025-02-1313:23:58
  • 转载请务必保留本文链接:http://wp.fangfa.me/other-note/python%e4%b8%8b%e8%bd%bd%e6%96%87%e4%bb%b6%e7%9a%84%e5%87%a0%e7%a7%8d%e5%b8%b8%e7%94%a8%e6%96%b9%e6%b3%95-%e7%8e%8b%e4%bc%9f-51cto%e6%8a%80%e6%9c%af%e5%8d%9a%e5%ae%a2.html