Python requests with Cookie access page

Source: Internet
Author: User


Background:
Get the page you need to sign in to add the cookie you acquired to the requested script after logging in.



Method:

Import requests
Url= "Http://webserver/admin"

cookies = dict (cookies_are= ' cookie content ')

# #cookie内容可在登陆页面后, obtained through the browser tool ().

Response = Requests.get (URL, cookies=cookies)

Print Response.content.decode ("Utf-8")


This allows you to get the Web page source code after landing.


If you want to simulate a more realistic point, it would be better to add the user agent.
My_headers = {' user-agent ': ' mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/45.0.2526.80 safari/537.36 core/1.45.933.400 qqbrowser/ 9.0.8699.400 ', ' accept-encoding ': ' gzip, deflate, SDCH '}

Response = Requests.get (URL, cookies=cookies,headers=my_headers)

Print Response.content.decode ("Utf-8")


As long as it is not specifically targeted, it should generally be treated as normal access by the server.





This article from "Ops said: from rookie to veteran" blog, please be sure to keep this source http://liuqunying.blog.51cto.com/3984207/1836309

Python requests with Cookie access page

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.