一、下载文件代码
这里使用requests下载文件,设置stream=True参数的意义在于先不拿取响应数据,只取header响应头数据中的文件大小信息。文件写入模式为ab+,意思为将二进制流的形式写入到文件,且不需要关注光标位置,自动累加到文件最后,且不覆盖之前的信息。
import time
import random
import requests
res = requests.get('https://pypi.doubanio.com/packages/79/87/8bb36bd4ebae147612c73d1bdc1385db7beebb9eb141c4bfefb33f52c87c/pandas-1.1.5-cp36-cp36m-win_amd64.whl#sha256=19a2148a1d02791352e9fa637899a78e371a3516ac6da5c4edc718f60cbae648', stream=True)
size = int(res.headers['content-length']) // 1024
def percent(p, b):
print(f'\r下载中:{"▒" * p}{b}%', end='')
def download():
b = []
with open('pandas-1.1.5-cp36-cp36m-win_amd64.whl', 'ab+') as f:
for i, chunk in enumerate(res.iter_content(chunk_size=1024)):
if chunk:
b.append(i)
bf = int(len(b)/size*100)
per = int(bf/4)
percent(per, bf)
f.write(chunk)
else:
print('下载失败!')
break
download()
二、下载成功打印信息
下载中:▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒100%
Process finished with exit code 0
三、使用安装测试文件是否正确
C:\Users\Administrator\Desktop\program>pip install pandas-1.1.5-cp36-cp36m-win_amd64.whl
Looking in indexes: https://pypi.douban.com/simple/
Processing c:\users\administrator\desktop\program\pandas-1.1.5-cp36-cp36m-win_amd64.whl
Requirement already satisfied: python-dateutil>=2.7.3 in c:\users\administrator\appdata\local\programs\python\python36\lib\site-packages (from pandas==1.1.5) (2.8.1)
Requirement already satisfied: numpy>=1.15.4 in c:\users\administrator\appdata\local\programs\python\python36\lib\site-packages (from pandas==1.1.5) (1.19.5)
Requirement already satisfied: pytz>=2017.2 in c:\users\administrator\appdata\local\programs\python\python36\lib\site-packages (from pandas==1.1.5) (2021.1)
Requirement already satisfied: six>=1.5 in c:\users\administrator\appdata\local\programs\python\python36\lib\site-packages (from python-dateutil>=2.7.3->pandas==1.1.5) (1.16.0)
Installing collected packages: pandas
Successfully installed pandas-1.1.5
nice, o jb k