import requests from retrying import retry requests.packages.urllib3.disable_warnings() # 静态变量 BASE_URL = 'https://pan.baidu.com' ERROR_CODES = { 1: '链接失效,没获取到 shareid', 2: '链接失效,没获取到 user_id', 3: '链接失效,没获取到 fs_id', '百度网盘-链接不存在': '链接失效,文件已经被删除或取消分享', '百度网盘 请输入提取码': '链接错误,缺少提取码', -9: '链接错误,提取码错误或验证已过期', -62: '链接错误尝试次数过多,请手动转存或稍后再试', 105: '链接错误,链接格式不正确', -4: '转存失败,无效登录。请退出账号在其他地方的登录', -6: '转存失败,请用浏览器无痕模式获取 Cookie', 4: '转存失败,目录中已有同名文件或文件夹存在', -8: '转存失败,目录中已有同名文件或文件夹存在', 12: '转存失败,转存文件数超过限制', -7: '转存失败,秒传文件名有非法字符', 404: '转存失败,秒传无效', 31190: '转存失败,秒传未生效', 31039: '转存失败,秒传文件名冲突', -10: '转存失败,容量不足', 20: '转存失败,容量不足', 0: '转存成功', } class ReqAction: # 请求变量 request_header = { 'Host': 'pan.baidu.com', 'Connection': 'keep-alive', 'Upgrade-Insecure-Requests': '1', 'Sec-Fetch-Dest': 'document', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9', 'Sec-Fetch-Site': 'same-site', 'Sec-Fetch-Mode': 'navigate', 'Referer': 'https://pan.baidu.com', 'Accept-Encoding': 'gzip, deflate, br', 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-US;q=0.7,en-GB;q=0.6,ru;q=0.5', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36', } def __init__(self): sess = Request @retry(stop_max_attempt_number=3, wait_fixed=1000) def get_bdstoken(self): url = f'{BASE_URL}/api/gettemplatevariable?clienttype=0&app_id=250528&web=1&fields=[%22bdstoken%22,%22token%22,%22uk%22,%22isdocuser%22,%22servertime%22]' response = self.session.get(url=url, headers=self.request_header, timeout=20, allow_redirects=True, verify=False) return response.json()['errno'] if response.json()['errno'] != 0 else response.json()['result']['bdstoken']