Python:requests Module

Source: Internet
Author: User

The Requests module is a module that I learned when I learned the crawler, its API is relatively simple and easy to use, here is the introduction of the method.

In fact, this is good to use, a few lines of code can get the content of a Web page:

Import Requestsurl = ' http://www.juzimi.com/ju/252304 ' response = requests.get (URL) print (response.text)

It supports many HTTP request Types : Get,post,put,Delete ,head,options

There is a 2 display method for the response content

. Content displayed in bytes, Chinese as characters

. Text in the form of text, two pictures will make sense.

this is content.

This is text

Requests will automatically decode the content from the server. Most Unicode character sets can be decoded seamlessly.

You can see What encoding the requests uses by. Encoding

You can also manually change the encoding that it uses r.encoding= ' gbk2312 '

Get requests can pass parameters

Import Requestsurl = ' http://www.juzimi.com/article/33125 ' payload = {' page ': ' 1 '}response = Requests.get (url,params= Payload) print (Response.text)

You can print a. URLto view the constructed URL

Customizing the request Header

Pass a dict to the heads parameter

headers = {' user-agent ': ' my-app/0.0.1 '}

r = Requests.get (URL, headers=headers)

send a post request

Payload = {' Key1 ': ' value1 ', ' key2 ': ' value2 '}

r = Requests.post ("Http://httpbin.org/post", Data=payload)

The Get method also has a cookie parameter

Timeout parameter

Access Proxy

Proxies = {           "http""http://10.10.10.10:8888" ,            " HTTPS " " http://10.10.10.100:4444 " ,           = Requests.get ('http://m.ctrip.com', proxies=proxies)

Python:requests Module

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.