Python learning-Log in with session and automatically post

Source: Internet
Author: User

1, first manually login, open the Fiddler grab bag, will be logged before and after the cookie to compare, find out the different records

2. The login code is as follows

Import Requestsimport logginglogging.capturewarnings (True) # Open the login first page, get some cookieurl = "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 "} #开启一个session会话s = Requests.session () R = S.get (url=url,headers=headers,ver Ify=false) print (s.cookies) #创建一个cookie对象c = Requests.cookies.RequestsCookieJar () #添加登陆中需要的两个cookiec. Set ('. Cnblogscookie ', ' E03f2436fa60a1d4dc98d0f772c86d8235401909810eaf6cb131808ae341d1a4044f70c2d8fc245d088d26a6d9d0cabc08118b4771ff57ebb9eaf90c6 Da075c49b010fbc0bb6ce80eb0d9d2f4570c6d5283b0c17 ') # Fill the above grab bag content c.set ('. Cnblogs.AspNetCore.Cookies ', ' Cfdj8fhxrrtkjwrftu30nh_ M9mchys2bw8clxxdqlqmnnxsjxmn5ojsmwovhs0divq1dxty6lsewu9x-zc_ Oy0upwnl1prycpomkfichuluxxi7xfafeetn5v26kzjougaryhf5x23edor56fltbv8vlipwgonu4-zeqbnpgwhfpyyuzqe7bl9h41fw00bzv0izforvwwudh Tptg5w0wsi7h77rxdbovrsune04nnkkp46dkhssqhfj3d5_zct8ifjnmvryk-4bdokorluy69qhtzvpf0vda2fcyw0sesy_ Xeh1uigeqk-jqw2tzdbuq_m0xxq ') # Fill in the top grab bagTolerant C.set (' alwayscreateitemsasactive ', ' true ') c.set (' admincookiealwaysexpandadvanced ', "true") # Update saved cookie Information s.cookies.update (c) print (s.cookies)

3, after landing into the post page, first manually click, enter the page, find the URL of the page, and then manually enter the content to fill in after the submission, grab the packet to see the data submitted, the data is written in a dictionary format, the final use of the POST request to submit content, the code is as follows:

#登陆成功之后保存编辑内容r1 = S.get (' https://i.cnblogs.com/EditPosts.aspx?opt=1 ', headers = headers,verify=false) #保存草稿内容url1 = ' Https://i.cnblogs.com/EditPosts.aspx?opt=1 ' BODY = {    ' __viewstate ': ', '    __viewstategenerator ': ' fe27d343 ', '    editor$edit$txbtitle ': ' This is full of ', '    editor$edit$editorbody ': ' <p> full Test </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$txbtag ': ',    ' editor$edit$ Advanced$tbenrypassword ': ', '    editor$edit$lkbdraft ': ' Save as draft '}R2 = S.post (url1,data=body) print (R.status_code)

4, after the successful submission, manually enter the blog park, open the draft box, you will find that we have submitted content has been shown in the draft box

Python learning-Log in with session and automatically post

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.