Request module in Python Learn "in the middle"

Source: Internet
Author: User

Installation:

PIP Install requests

Use:

Import requests

HTTP requests: GET, POST, PUT, DELETE, HEAD, OPTIONS

1) Get

res = Requests.get ("Https://github.com/timeline.json")

2) Post

res = Requests.post ("Http://httpbin.org/post");

3) put

res = Requests.put ("Http://httpbin.org/put");

4) Delete

res = Requests.delete ("Http://httpbin.org/delete");

5) Head

res = Requests.head ("Http://httpbin.org/get");

6) Options

res = requests.options ("Http://httpbin.org/get")

Passing parameters for URLs

The requests module provides parameters in the form of a dictionary using the params keyword parameter.

>>> payload = {' Key1 ': ' Value ', ' key2 ': ' value2 '}
>>> res = requests.get ("Http://httpbin.org/get", Params=payload)
>>> Res.url
U ' http://httpbin.org/get?key2=value2&key1=value '

View response Content

>>> res = requests.get ("Http://github.org/timeline.json")
>>> Res.text
U ' {"message": "Hello there, Wayfaring stranger. If You\u2019re Reading this and you probably didn\u2019t see we blog post a couple of years back announcing that this AP I would go Away:http://git.io/17arog fear not, you should is able to get what is need from the shiny new Events API Inst EAD. "," Documentation_url ":" Https://developer.github.com/v3/activity/events/#list-public-events "} '

The requests module automatically decodes the content from the server, You can use Res.encoding to view the encoding, and, if you need to, request can also use a custom encoding and register with the codes module, so that you can easily use the name of the decoder as the res.encoding value

>>> res.encoding
' Utf-8 '
>>> res.encoding = "gbk2312"

Get the contents of a response in JSON format

>>> res = requests.get ("Http://github.org/timeline.json")
>>> Res.json ()
{u ' documentation_url ': U ' https://developer.github.com/v3/activity/events/#list-public-events ', u ' message ': U ' Hello there, Wayfaring stranger. If You\u2019re Reading this and you probably didn\u2019t see we blog post a couple of years back announcing that this AP I would go Away:http://git.io/17arog fear not, you should is able to get what is need from the shiny new Events API Inst EAD. '}

The original should be content

>>> res = requests.get ("Http://github.org/timeline.json")
>>> Res.raw
<requests.packages.urllib3.response.httpresponse Object at 0x0000000002f31550>
>>> Res.raw.read (10)
‘‘

Custom Request Headers

>>> Import JSON
>>> url = ' Https://api.github.com/some/endpoint '
>>> payload = {' Some ': ' Data '}
>>> headers = {' Content-type ': ' Application/json '}
The POST request parameter is this:
>>> payload = {' Key1 ': ' value1 ', ' key2 ': ' value2 '}
>>> url = "Http://httpbin.org/post"
>>> Res =requests.post (url,data=payload)
>>> Res.text
U ' {\ n ' args ': {}, \ n "Data": "", \ n "files": {}, \ n "form": {\ n "key1": "value1", \ n "key2": "value2" \ n}, \ n "Headers": {\ n "Accept": "*/*", \ n "accept-encoding": "gzip, deflate", \ n "content-length": "All", \ n "content- Type ":" "application/x-www-form-urlencoded", \ n "Host": "httpbin.org", \ n "user-agent": "python-requests/2.10.0" \ n} , \ n "JSON": null, \ n "origin": "218.240.129.20", \ n "url": "Http://httpbin.org/post" \n}\n "
>>> Print Res.text
{
"args": {},
"Data": "",
"Files": {},
"Form": {
"Key1": "Value1",
"Key2": "value2"
},
"Headers": {
"Accept": "*/*",
"Accept-encoding": "gzip, deflate",
"Content-length": "23",
"Content-type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-agent": "python-requests/2.10.0"
},
"JSON": null,
"Origin": "218.240.129.20",
"url": "Http://httpbin.org/post"
}

Response status Code and response header:

>>> res = requests.get ("Http://httpbin.org/get")

>>> Res.status_code
200
>>> Res.status_code = = Requests.codes.ok
True
>>> res.headers
{' content-length ': ' 239 ', ' Server ': ' Nginx ', ' Connection ': ' keep-alive ', ' access-control-allow-credentials ': ' True ', ' Date ': ' Sun, 09:24:10 GMT ', ' access-control-allow-origin ': ' * ', ' content-type ': ' Application/json '}
>>> Print Res.headers
{' content-length ': ' 239 ', ' Server ': ' Nginx ', ' Connection ': ' keep-alive ', ' access-control-allow-credentials ': ' True ', ' Date ': ' Sun, 09:24:10 GMT ', ' access-control-allow-origin ': ' * ', ' content-type ': ' Application/json '}
>>> res.headers[' Content-type ']
' Application/json '
>>> res.headers.get (' Content-type ')
' Application/json '
>>> res.headers.get (' Connection ')
' Keep-alive '
>>>

Cookies:

Access to Cookies

>>> url = ' Http://example.com/some/cookie/setting/url '
>>> r = requests.get (URL)

>>> r.cookies[' Example_cookie_name ']

Set cookies

>>> url = ' http://httpbin.org/cookies '
>>> cookies = dict (cookies_are= ' working ')

>>> r = requests.get (URL, cookies=cookies)
>>> R.text

' {' "cookies": {"Cookies_are": "Working"}} '

Request module in Python learn "in layman's language"

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.