python通过百度云api的方式上传或下载文件

1、参考网址:https://github.com/ly0/baidupcsapi

https://blog.csdn.net/sinat_28731575/article/details/72722700

2、编写一个脚本实现长传图片,命名为:pic_upload.py

import progressbar
from baidupcsapi import PCS
class ProgressBar():
    def __init__(self):
        self.first_call = True
    def __call__(self, *args, **kwargs):
        if self.first_call:
            self.widgets = [progressbar.Percentage(), ' ', progressbar.Bar(marker=progressbar.RotatingMarker('>')),
                            ' ', progressbar.ETA()]
            self.pbar = progressbar.ProgressBar(widgets=self.widgets, maxval=kwargs['size']).start()
            self.first_call = False

        if kwargs['size'] <= kwargs['progress']:
            self.pbar.finish()
        else:
            self.pbar.update(kwargs['progress'])


pcs = PCS('杜如意8','dxy199113')
test_file = open('example_04.jpg','rb').read()
ret = pcs.upload('/',test_file,'example_04.jpg',callback=ProgressBar())

3、在虚拟环境pi_py3环境下运行:

python3 pic_upload.py

要输入图片的验证码,还要接收邮箱的数字验证码,有些麻烦

猜你喜欢

转载自blog.csdn.net/weixin_41946146/article/details/88818239