Objective
On a simulation login blog park, but this is only the first step, general login, there will be other operations, such as posts, comments, etc., how to maintain the session?
I. Introduction of the SESSION
1. View the Help document, paste a part, and omit the following
>>import Requests
>>help (Requests.session ())
Class Session (Sessionredirectmixin)
| A requests session.
|
| Provides cookie persistence, connection-pooling, and configuration.
|
| Basic Usage::
|
| >>> Import Requests
| >>> s = requests. Session ()
| >>> s.get (' Http://httpbin.org/get ')
| <response [200]>
|
| Or as a context Manager::
|
| >>> with requests. Session () as S:
| >>> s.get (' Http://httpbin.org/get ')
| <response [200]>
Second, use the session login
1. Use session login just to make a slight change on the previous article
# Coding:utf-8
Import requests
url = "Https://passport.cnblogs.com/user/signin"
headers = {
The header information has been omitted
}
Payload = {"INPUT1": "XXX",
"Input2": "XXX",
"Remember": True}
# r = requests.post (URL, json=payload, headers=headers,verify=false)
# modified as follows
s = requests.session ()
r = S.post (URL, json=payload, headers=headers,verify=false)
Print R.json ()
Third, save the editor
1. Open My essay first, enter the content manually, open the Fiddler grab Bag
2. The body parameter content is written in a dictionary format, there are several empty parameters are not required, you can remove
BODY = {"__viewstate": "",
"__viewstategenerator": "fe27d343",
"Editor$edit$txbtitle": "This is my title: Shanghai-Leisurely",
"Editor$edit$editorbody": "<p> here is Chinese content:http://www.cnblogs.com/yoyoketang/</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",
}
3. Continue sending the POST request with the session above
4. After the execution, check my drafts box and add a new one.
Iv. Reference Code
# Coding:utf-8
Import requests
url = "Https://passport.cnblogs.com/user/signin"
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 ",
# "Verificationtoken": "xxx ...", # omitted
"X-requested-with": "XMLHttpRequest",
# "Referer": "Https://passport.cnblogs.com/user/signin?" Returnurl=http%3a%2f%2fmsg.cnblogs.com%2fsend%2f%e4%b8%8a%e6%b5%b7-%e6%82%a0%e6%82%a0 ",
"Content-length": "385",
"Cookie": "xxx ...", # omitted
"Connection": "Keep-alive"
}
# Login Parameters
Payload = {"INPUT1": "xxx",
" Input2 ":" xxx ",
" Remember ": True}
S = Requests.session ()
R = s.post (URL, json=payload, headers=headers,verify=false)
Print R.json ()
# Save Drafts box
url2= "https://i.cnblogs.com/EditPosts.aspx?opt=1"
Body = {"__viewstate": "",
"__viewstategenerator": "fe27d343",
"Editor$edit$txbtitle": "This is my title: Shanghai-leisurely",
"editor$edit$editorbody": " <p> here is Chinese content:http://www.cnblogs.com/yoyoketang/</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 r.content
Here I write a case of saving a draft case, the small friends can try to post automatically
(Note: Do not use too often oh, carefully seal the number Hey!!!) )
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 3-Auto Post (session)