最近在设计本站PDF电子书页面的时候发现针对百度云PDF目录下的电子书生成不加密的共享连接很不方便。json
有人可能问那么多文件生成一个共享连接供你们下载那多方便?不存在的,防止大家一次性保存到本身百度云而后跑路。api
每次都要一个一个右键共享生成,这样不只容易出错,眼花缭乱,并且效率低,浪费时间等等。因此打算经过程序实现,正好百度了一下,发现有Python对应的百度云的API,正好练练手。cookie
Python3.5.3,Windows操做系统,pycharm,Python包(setuptools,pyinstaller,baidupcsapi等反正缺啥模块安啥模块)app
打开pycharm,新建Python文件,命名为BaiduYunLinksShare.py函数
from baidupcsapi import PCS import time import json
def __init__(self, username, password, sharedir): self.username = username self.passwrod = password self.sharedir = sharedir encrypt_password = "%s*****%s" % (password[:2], password[-2:]) print("您输入的帐号:%s,密码:%s,须要批量生成共享链接的目录是:%s" % (username, encrypt_password, sharedir))
解释:学习
def client(self): return PCS(self.username, self.passwrod)
解释:加密
生成cookie文件截图spa
第一次输入验证码截图操作系统
def input_path_exist(self, client): input_path_exist_flag = client.list_files(self.sharedir, "size", "asc").json().get('errno') if input_path_exist_flag == -9: # errno=-9 目录不存在 不然返回该目录下的文件详情 return False else: return pcs.list_files(self.shardir, "size", "asc").json().get('list')
解释:设计
baiduyun = BaiduYunLinksShare(username, password, dir_path) pcs = baiduyun.client() input_path_exist = baiduyun.input_path_exist(pcs) file_ids_list = [] if input_path_exist is False: print(">>>>>您输入的须要批量生成共享链接的目录不存在") else: for i, file in enumerate(input_path_exist): file_ids_list.append(file.get('fs_id')) res_json = json.loads(pcs.share(file_ids_list).text) if res_json['errno'] != 0: print(">>>>>您今天的分享次数可能已经达到上限") break else: res = "序号:[%s] 共享文件:%s 共享连接:%s" % (i, file.get('server_filename'), res_json['link']) print(res) try: with open(r'D:\baiduyun_share.txt', 'a+', encoding='UTF-8') as f: f.write(res) f.write('\n') file_ids_list = [] time.sleep(2) except Exception as e: print(str(e)) else: f.close() print(">>>>>连接已经保存在D:\\baiduyun_share.txt文件中")
解释:
以上就是批量实现共享百度云某个目录下的文件连接的主要代码了,仅供我的学习。