百度云删除文件API接口探索
通过抓包发现百度云删除文件接口https://pan.baidu.com/api/filemanager,本文主要试图通过该接口来封装一个删除百度网盘文件的api接口。
浏览器请求数据包信息如下:
浏览器响应信息如下:
上述响应信息说明文件删除成功
代码如下:
1 #! /usr/bin/python3 2 # -*- coding: UTF-8 -*- 3 4 import time, re, requests, json 5 from selenium.webdriver import Firefox, Chrome 6 from selenium.webdriver.common.by import By 7 from selenium.webdriver.common.keys import Keys 8 from selenium.webdriver.firefox.options import Options 9 from selenium.webdriver.support import expected_conditions as expected 10 from selenium.webdriver.support.wait import WebDriverWait 11 12 def baiduyun_test (): 13 print (time.ctime ()) 14 browser = Chrome() 15 try: 16 browser.get('https://pan.baidu.com/') 17 time.sleep (3) 18 #点击"账号密码登陆" 19 elem = browser.find_element_by_xpath('//p[@class="tang-pass-footerBarULogin pass-link"]') 20 elem.click () 21 print ('点击"账号密码登陆"') 22 #输入账号 23 elem = browser.find_element_by_id('TANGRAM__PSP_4__userName') 24 elem.clear () 25 elem.send_keys('username') 26 print ('输入账号') 27 #time.sleep (3) 28 #输入密码 29 elem = browser.find_element_by_id('TANGRAM__PSP_4__password') 30 elem.clear () 31 elem.send_keys('password') 32 print ('输入密码') 33 time.sleep (1) 34 #点击登陆 35 elem = browser.find_element_by_id('TANGRAM__PSP_4__submit') 36 elem.click () 37 print ('点击登陆') 38 time.sleep (3) 39 #获取bdstoken 40 pattern = r'"bdstoken":"(.*?)"' 41 while True: 42 try: 43 page_html = browser.page_source 44 bdstoken = re.findall (pattern, page_html) [0] 45 break 46 except: 47 print ('等待验证~~~') 48 time.sleep (3) 49 print ('bdstoken :', bdstoken) 50 logid = 'MTUzNDM4NDk3MjYzNDAuNTAyODg4NzM4MTQyNDE0Nw==' 51 url = 'https://pan.baidu.com/api/filemanager?opera=delete&async=2&onnest=fail&channel=chunlei&web=1&app_id=250528&bdstoken=' + bdstoken + '&logid=' + logid + '&clienttype=0' 52 print ('url :', url) 53 #获取cookies 54 Cookie = '' 55 for item in browser.get_cookies (): 56 if item ['name'] == 'BDUSS' or item ['name'] == 'STOKEN': 57 Cookie += item ['name'] + '=' + item ['value'] + ';' 58 Cookie.rstrip (';') 59 #print ('Cookie :', Cookie) 60 headers = { 61 "Accept": "application/json, text/javascript, */*; q=0.01", 62 "Accept-Encoding": "gzip, deflate, br", 63 "Accept-Language": "zh-CN,zh;q=0.9", 64 "Connection": "keep-alive", 65 "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8", 66 "Cookie": Cookie, 67 "Host": "pan.baidu.com", 68 "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36", 69 #"X-Requested-With": "XMLHttpRequest" 70 } 71 data = { 72 "filelist" : ["/test.jpg"] 73 } 74 response = requests.post (url = url, data = data, headers = headers) 75 print (response.content) 76 except BaseException as e: 77 print (e) 78 finally: 79 browser.quit () 80 print (time.ctime ())
代码介绍:
首先通过使用Python3的selenium模块进行模拟百度云登录来获取删除百度云文件接口中所需的各项参数,主要包含bdstoken、BDUSS和STOKEN,bdstoken包含在页面HTML代码中,BDUSS和STOKEN则包含在Cookie中;
然后通过上述各项参数构造POST请求,完全模拟浏览器删除文件操作。
目前遇到的问题:但即使我构造了跟浏览器一模一样的请求头和cookie等信息,仍然会报错
报错信息如下:
{"errno":2,"info":[],"request_id":5310516426993481489}
Google得知为参数错误(参考链接:[Callback] 百度网盘errno错误代码汇总对照表)
参考链接:如何清除百度网盘重复文件
08.18更新
在Tychxn的帮助下,成功发现问题并解决,具体过程如下
在此,向Tychxn致谢,其GitHub地址为https://github.com/tychxn。