Python module-requests (i)

Source: Internet
Author: User

Requests is not available in Python and needs to be installed before use

    • Send Request

HTTP request types are get,post,put,delete,head and options

Here's how to send a request using requests:

>>> Import requests>>> r = Requests.get ("http://httpbin.org/get") #发送GET请求 >>> r = Requests.post ("Http://httpbin.org/post") #发送POST请求 >>> r = requests.put ("Http://httpbin.org/put") #发送PUT请求 >>> r = Requests.delete ("Http://httpbin.org/delete") #发送DELETE请求 >>> r = Requests.head ("http/ Httpbin.org/get ") #发送HEAD请求 >>> r = requests.options (" Http://httpbin.org/get ") #发送OPTIONS请求
    • Passing URL parameters

The params parameter will stitch the incoming parameters

The format of a dictionary is usually used with params passed as a parameter

>>> Import requests>>> payload = {"word": "Test", "page":11}>>> r = Requests.get ("/HTTP/ Httpbin.org/get ", params=payload) >>> print (R.url) #打印r里的url参数的值http://httpbin.org/get?word=test&page =11

The value in the dictionary can also be a list

>>> payload = {"word": "Test", "page":[1,2,3]}>>> r = Requests.get ("Http://httpbin.org/get", params= Payload) >>> print (R.url) http://httpbin.org/get?word=test&page=1&page=2&page=3

A key with a value of none in the dictionary will not be passed to the URL.

>>> payload = {"word": "Test", "page":none}>>> r = Requests.get ("Http://httpbin.org/get", params= Payload) >>> print (R.url) http://httpbin.org/get?word=test

Parameters passed by params can also be directly string

>>> payload = "word=test&page=11" >>> r = Requests.get ("Http://httpbin.org/get", Params=payload) >>> print (R.url) http://httpbin.org/get?word=test&page=11
    • Response Content

Requests can read the contents of the server response

>>> r = Requests.get ("https://www.cnblogs.com/") >>> r.text #获取网页源代码 "" Here is the source code for the Web page ">>> R.encoding #查看网页源代码的编码 ' utf-8 ' >>> r.encoding = ' GBK ' #把网页源码的编码改为gbk >>> r.encoding #再调用的时候, found that the page encoding became GBK ' GBK '
    • Binary response Content

For non-textual requests, requests can also access the request response body in bytes

>>> r = Requests.get ("http://p1.ifengimg.com/a/2018_06/75880eeacd0823d_size11_w230_h152.jpg") >> > r.content "" Here for bytes type of picture content ' ' >>> r.text ' ' A bunch of garbled '

This method can also be used for text requests, but the returned result is of type bytes

>>> r = Requests.get ("https://www.cnblogs.com/") >>> R.text "' Here is the text type of the Web page source code ' >>> R.content "' Here is the bytes type of Web page source code '
    • JSON response content

Requests also has a built-in JSON decoder that helps us work with JSON data

>>> Import requests>>> r = Requests.get ("Https://github.com/timeline.json") >>> R.json () {' Message ': ' Hello there, wayfaring stranger.  If you ' re reading this and you probably didn ' t see our blog post a couple of years back announcing so this API would go Away:http://git.io/17arog fear not, you should is able to get the "need from the shiny new Events API instead. ' Cumentation_url ': ' https://developer.github.com/v3/activity/events/#list-public-events '}>>> r.status_ Code410>>> R.raise_for_status<bound method Response.raise_for_status of <response [410]>>

If the JSON data decoding fails, it throws an valueerror:no JSON object could be decoded exception

However, successful invocation of R.json () does not indicate a successful response, and some servers include a JSON object in the failed response, such as the error details of HTTP 500, which is also decoded back

So to check if the request is successful, you can use R.status_code and r.raise_for_status to check

    • Original response Content

Requests getting the original socket response from the server

>>> Import requests>>> r = Requests.get ("Http://httpbin.org/get", stream=true) >>> R.raw <urllib3.response.httpresponse object at 0x000001b93f230518>>>> r.raw.read b ', \ n    " Accept-encoding ":" gzip, deflate ", \ n    " Connection ":" Close ", \ n    " Host ":" httpbin.org ", \ n    " user-agent ":" python-requests/2.18.4 "\ n  }, \ n  " origin ":" 110.90.39.155 ", \ n  " url ":" Http://httpbin.org/get "\n}\n '

To set stream=true in the initial request and then use R.raw, you can read the content using R.raw.read ()

Python module-requests (i)

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.