Objective
The previous article implemented the parameters of the association, the kind of just remember the completion of the journal function, inconvenience to maintenance, but also no readability, the next one can be written into a function of each action, it is more convenient.
Parametric thinking just remember a little bit: Don't write dead
First, login function
1.S parameter is an instance of the session class, put here, convenient to write the following code
2. Login function to pass three parameters, S is required to call the previous session class, so must pass, you can pass a login URL, and then payload is the account and password
Ii. Preservation of drafts
1. Title and body of the edited content Body_data parameterized, so that you can easily pass different values later
2. This returns the fetch to the new URL address, since the following PostID parameter needs to be extracted here
Iii. Extraction of PostID
1. Use the regular expression to extract the PostID inside the URL.
Iv. deletion of drafts
1. Pass a URL and PostID.
Five, reference code:
# Coding:utf-8
Import requests
def login (s, URL, payload):
"Login"
headers = {"User-agent": "mozilla/5.0 (Windows NT 10.0; WOW64; rv:44.0) gecko/20100101 firefox/44.0 ",
"Accept": "Application/json, Text/javascript, */*; q=0.01 ",
"Accept-language": "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3",
"Accept-encoding": "gzip, deflate, BR",
"Content-type": "Application/json; Charset=utf-8 ",
"X-requested-with": "XMLHttpRequest",
"Content-length": "385",
"Cookie": "XXX has been omitted",
"Connection": "Keep-alive"
}
r = S.post (URL, json=payload, headers=headers, Verify=false)
result = R.json ()
Print result
Return result[' success ') # returns TRUE or False
def save_box (S, url2, title, Body_data):
' # Get the URL address after the deposit '
BODY = {"__viewstate": "",
"__viewstategenerator": "fe27d343",
"Editor$edit$txbtitle": Title,
"Editor$edit$editorbody": "<p>" +body_data+ "</p>",
"Editor$edit$advanced$ckbpublished": "On",
"Editor$edit$advanced$chkdisplayhomepage": "On",
"Editor$edit$advanced$chkcomments": "On",
"Editor$edit$advanced$chkmainsyndication": "On",
"Editor$edit$lkbdraft": "Save as draft",
}
r2 = S.post (Url2, Data=body, Verify=false)
Print R2.url
Return R2.url
def get_postid (U):
"' Regular extraction of PostID '
Import re
PostID = Re.findall (r "postid= (. +?) & ", u)
Print PostID # here is the list
If Len (PostID) < 1:
Return '
Else
return postid[0]
def delete_box (S,url3, PostID):
"Delete a draft box"
Json3 = {"PostID": PostID}
R3 = S.post (Url3, Json=json3, Verify=false)
Print R3.json ()
if __name__ = = "__main__":
url = "Https://passport.cnblogs.com/user/signin"
Payload = {
"INPUT1": "XXX",
"Input2": "XXX",
"Remember": True
}
s = requests.session ()
Login (s, URL, payload,)
URL2 = "Https://i.cnblogs.com/EditPosts.aspx?opt=1"
U = Save_box (S, Url2, "title", "Body content")
PostID = Get_postid (u)
Url3 = "Https://i.cnblogs.com/post/delete"
Delete_box (S, Url3, PostID)
interested in Python interface automation, you can add Python interface Automation QQ Group: 226296743
You can also pay attention to my personal public number:
Python interface Automation 8-parameterization