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"
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)
|
具体详情可参见:http://www.blog.pythonlibrary.org/2012/06/07/python-101-how-to-download-a-file/
本文转自 lover00751CTO博客,原文链接:http://blog.51cto.com/wangwei007/1351429,如需转载请自行联系原作者