Python interface Automation 3-Auto Post (session) "Reprint"

Source: Internet
Author: User

From the blog: Shanghai-leisurely

Original address: http://www.cnblogs.com/yoyoketang/tag/python%E6%8E%A5%E5%8F%A3%E8%87%AA%E5%8A%A8%E5%8C%96/

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?

(Knock the blackboard!!!) Because the blog park recently login mechanism changed, login all go cookie login)

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

# Open the Login first page to get some cookies
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 "
} # Get method add a ser-agent to it.

s = requests.session ()
r = S.get (URL, headers=headers,verify=false)
Print S.cookies

# Two cookies required to add a login
c = Requests.cookies.RequestsCookieJar ()

C.set ('. Cnblogscookie ', ' Here is caught ') # fill up the contents of the grab Bag
C.set ('. Cnblogs.AspNetCore.Cookies ', ' Here is caught ') # fill up the contents of the grab Bag
C.set (' alwayscreateitemsasactive ', "True")
C.set (' admincookiealwaysexpandadvanced ', "True")
S.cookies.update (c)
Print S.cookies

# Save edits after successful login
R1 = S.get ("Https://i.cnblogs.com/EditPosts.aspx?opt=1", Headers=headers, Verify=false)

# Save Drafts Box
Url2= "Https://i.cnblogs.com/EditPosts.aspx?opt=1"
BODY = {"__viewstate": "",
"__viewstategenerator": "fe27d343",
"Editor$edit$txbtitle": "This is 3111",
"Editor$edit$editorbody": "<p> here 111: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$advanced$txbentryname": "",
"Editor$edit$advanced$txbexcerpt": "",
"Editor$edit$advanced$tbenrypassword": "",
"Editor$edit$lkbdraft": "Save as draft",
}
r2 = S.post (Url2, Data=body, Verify=false)
Print R.content

Python interface Automation 3-Auto Post (session) "Reprint"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.