Python uses the requests module to complete get/post/proxy/custom header/custom Cookies

Source: Internet
Author: User

I. BACKGROUND notes

Because Python3 's urllib and Python2 have a lot of differences, and some of the urllib.request request is not quite in line with people's thinking habits (documents are quite ugly)

So I really don't want to use Python until I look at the Urllib.request document and notice the words below

Compared to requests is really good, the document is also clear; you need to install it yourself, but it is still more recommended.

1.1 Applicable version

For python2.6, python2.7, python3.4 and above, see the official note.

I'm using the latest python3.7.

1.2 Installing the Requests module
PIP Install pipenvpipenv Install requests

Second, use requests module to complete various operations 2.1 reference requests module
Import Requests

2.2 GET Request
Import requestsurl='https://www.baidu.com'= requests.get (URL) Print (R.status_code)

2.3 Post Request
Import requestsurl='https://www.baidu.com'data_post='just Put your data and use original format'= requests.post (url,data=data_post)Print (R.status_code)

2.4 Using proxies

HTTP proxy no problem, HTTPS seems a bit of a problem need to test again

ImportRequestsurl='http://docs.python-requests.org/en/master/'proxies={    'http':'127.0.0.1:8080',    'HTTPS':'127.0.0.1:8080'}r= Requests.get (url,proxies=proxies)Print(R.status_code)

2.5 Customizing headers
ImportRequestsurl='http://docs.python-requests.org/en/master/'Headers={    'user-agent':'self-defind-user-agent',    ' COokie':'name=self-define-cookies-in Header'}r= Requests.get (url,headers=headers)Print(R.status_code)

2.6 Custom Cookies

The experiment found that cookies that are set here do not take effect if cookies are defined in the custom header

Import requests

Url= ' http://docs.python-requests.org/en/master/'
cookies={' name1 ': ' Cookie1 ', ' name2 ': ' Cookies2 '}
#cookies =dict (name1= ' cookie1 ', name2= ' cookies2 ')
R = Requests.get (url,cookies=cookies)
Print (R.status_code)

Reference:

Official Document--http://docs.python-requests.org/en/master/

Python uses the requests module to complete get/post/proxy/custom header/custom Cookies

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.