下面的Python脚本中利用request下载文件并写入到文件系统,利用progressbar模块显示下载进度条。
其中利用request模块下载文件可以直接下载,不需要使用open方法,例如:
1
2
3
4
5
6
7
|
import
urllib
import
requests.packages.urllib3
requests.packages.urllib3.disable_warnings()
url =
"https://raw.githubusercontent.com/racaljk/hosts/master/hosts"
urllib.urlretrieve(url, filename=
"hosts"
)
|
下面的例子是题目中完整的例子,其中注释的部分是进度条的另一种写法,显示当前处理过的行数。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
#!/usr/bin/python
# encoding: utf-8
# -*- coding: utf8 -*-
""
"
Created by PyCharm.
File: LinuxBashShellScriptForOps:download_file2.py
User: Guodong
Create Date: 2016
/9/14
Create Time: 9:40
""
"
import
requests
import
progressbar
import
requests.packages.urllib3
requests.packages.urllib3.disable_warnings()
url =
"https://raw.githubusercontent.com/racaljk/hosts/master/hosts"
response = requests.request(
"GET"
, url, stream=True, data=None, headers=None)
save_path =
"/tmp/hosts"
total_length = int(response.headers.get(
"Content-Length"
))
with
open
(save_path,
'wb'
) as f:
# widgets = ['Processed: ', progressbar.Counter(), ' lines (', progressbar.Timer(), ')']
# pbar = progressbar.ProgressBar(widgets=widgets)
# for chunk in pbar((i for i in response.iter_content(chunk_size=1))):
# if chunk:
# f.write(chunk)
# f.flush()
widgets = [
'Progress: '
, progressbar.Percentage(),
' '
,
progressbar.Bar(marker=
'#'
, left=
'['
, right=
']'
),
' '
, progressbar.ETA(),
' '
, progressbar.FileTransferSpeed()]
pbar = progressbar.ProgressBar(widgets=widgets, maxval=total_length).start()
for
chunk
in
response.iter_content(chunk_size=1):
if
chunk:
f.write(chunk)
f.flush()
pbar.update(len(chunk) + 1)
pbar.finish()
|
运行结果:
--end--
本文转自 urey_pp 51CTO博客,原文链接:http://blog.51cto.com/dgd2010/1852709,如需转载请自行联系原作者