Article From:https://www.cnblogs.com/countryKing/p/9495886.html

Baidu Cloud Delete File Interface https://pan.baidu.com/api/filemanager is found by capturing the package. This paper mainly attempts to encapsulate an API interface to delete Baidu disk files through this interface.

 

Browser request packet information is as follows:

Browser response information is as follows:

The above response information indicates that the file was deleted successfully.

The code is as follows:

 

 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         #Click on the account password to login.
19         elem = browser.find_element_by_xpath('//p[@class="tang-pass-footerBarULogin pass-link"]')
20         elem.click ()
21         print ('Click on the account password to login.')
22         #Input account number
23         elem = browser.find_element_by_id('TANGRAM__PSP_4__userName')
24         elem.clear ()
25         elem.send_keys('username')
26         print ('Input account number')
27         #time.sleep (3)
28         #Input password
29         elem = browser.find_element_by_id('TANGRAM__PSP_4__password')
30         elem.clear ()
31         elem.send_keys('password')
32         print ('Input password')
33         time.sleep (1)
34         #Click on the landing
35         elem = browser.find_element_by_id('TANGRAM__PSP_4__submit')
36         elem.click ()
37         print ('Click on the landing')
38         time.sleep (3)
39         #Get 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 ('Wait for verification ~ ~')
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         #Get 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 ())

 

Code Description:

First, we use Python 3’s selenium module to simulate Baidu Cloud Logon to get the parameters needed to delete Baidu Cloud File Interface, including bdstoken, BDUSS and STOKEN. bdstoken is included in the page HTML code.BDUSS and STOKEN are included in Cookie.

Then the POST request is constructed by the above parameters, which completely simulates browser delete file operation.

 

Current problems: But even if I construct the same request header and cookie information as the browser, I still report errors

The wrong information is as follows:

{“errno”:2,”info”:[],”request_id”:5310516426993481489}

GoogleLearn parameter error (reference link:[Callback] Baidu SkyDrive errno error code summary table

Reference link:How to remove duplicate files from Baidu SkyDrive

 

08.18To update

With the help of Tychxn, we successfully found and solved the problems.

Here, thanks to Tychxn, its GitHub address is https://github.com/tychxn.

Leave a Reply

Your email address will not be published. Required fields are marked *