Python Requests Library Translation (i) __python

Source: Internet
Author: User

Background: Always think of the Library of the request of the English version of translation, more is to take their own less commonly used methods to consolidate, recently not particularly busy on the translation of play it. In fact, there is a special detailed article on the Internet, as the digestion of their own understanding.

Start translating directly from the QuickStart module.

one. Send Request

To import the requests module first

>>> Import requests first get a Web page with the Get method to obtain the GitHub timeline. The Get method is the most commonly used method of the HTTP protocol for obtaining server resources.

>>> r = requests.get (' https://api.github.com/events ') obtains an instance of response called R through the Requests.get () method, We can use this object to get the information we want.

A simple API method for requests libraries that can easily send post requests:

>>> r = requests.post (' http://httpbin.org/post ', data = {' key ': ' Value '})

>>> Print R.json ()

{u ' files ': {}, U ' origin ': U ' 124.251.12.62 ', U ' form ': {u ' key ': U ' value '}, u ' url ': U ' http://httpbin.org/post ', U ' args ': {} , U ' headers ': {u ' content-length ': U ' 9 ', U ' accept-encoding ': U ' gzip, deflate ', U ' Host ': U ' httpbin.org ', U ' Accept ': U ' */* ' , u ' user-agent ': U ' python-requests/2.13.0 ', U ' Connection ': U ' close ', U ' content-type ': U ' application/ X-www-form-urlencoded '}, U ' json ': None, U ' data ': U '}

This simple post request, in addition to domain name: http://httpbin.org+ Interface name post, also has parameter data. Through the JSON () method, the returned response is a dictionary type

There are 5 types of HTTP requests, post,get,delete,head,option.

Get: For getting the contents of an interface.

Post: Bring the information to the interface and modify the corresponding information.

Head: Request to get the header information of the interface's response.

Delete: Deletes the corresponding incoming information.

Option: Get the performance of the server.

Note: I've used only post and get methods in this area.

two. Incoming Parameters

When sending a request, we want to pass in some parameters, we can use the params parameter, the data type is a dictionary type, such as the following:

>>> payload = {' Key1 ': ' value1 ', ' key2 ': ' value2 '}

>>> r = requests.get (' Http://httpbin.org/get ', params=payload)

>>> print R.json () can view the entire URL through the R.url properties

>>> R.url

U ' http://httpbin.org/get?key2=value2&key1=value1 '

three. Response Content

Through the. Text property We can view the response return text information

>>> r = requests.get (' https://api.github.com/events ')

>>> R.text requests supports decoding of response returned by the server, where the Unicode type is the most commonly used decoding format

Call the. Encoding property we can view the returned format

>>> r.encoding

' Utf-8 ' The returned format is utf-8 format

Of course, you can also customize the decoding format for example:

>>> r.encoding = ' iso-8859-1 ' If you want to change the encoding format, requests supports you to use r.encoding to set up a new encoding format, and then use R.text to verify that it is effective. In general, you first use R.context to view the encoding format, and then through the r.encoding to set up a new encoding, at the time to use R.text to verify that the setting is successful

four. Binary response content (this I did not understand, the direct translation of a moment, the current test is not used)

You can also return the body of response in binary format, rather than in text format

The "gzip" module can automatically encode the encoding mode automatically to decode it for you

Five. JSON format response content

You can use the JSON format to decode response, and then handle the corresponding key and value (the document Response.json () returns a list data type, but I do the interface test when the return is dict type)

>>> r = requests.get (' https://api.github.com/events ')

>>> R.json ()

Return is a list, too long to write, you have the interest to try it yourself

If the JSON decoding fails, the R.json () method throws an exception, for example: If response does not throw a 204, or if the return response is an invalid JSON format, it returns "Value Error: ..." (This I usually encounter is the interface to return a page)

Six. Original response content (no use, do not know when to use ...) )

In individual cases, we need to get the original response content, use the R.row attribute, and if you're sure you want to do this, you need to set the parameter stream=true when you need it.

When a normal GET request is set Stram=true:

>>> r = requests.get (' https://api.github.com/events ', stream=true)

>>> R.raw

<requests.packages.urllib3.response.httpresponse Object at 0x1055bb990>

>>> R.raw.read (10)

' \x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03 ' can read the contents normally.

If you do not set Stram=true:

>>> r = requests.get (' https://api.github.com/events ')

>>> R.raw

<requests.packages.urllib3.response.httpresponse Object at 0x1055aff90>

>>> R.raw.read (10)

"In this case read out

The following another article bar, write not move, to do other live, document look more also really can't carry ...




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.