Python interface 1---send GET request

Source: Internet
Author: User

Need to use the request module

Environment installation (provided that the PIP module has been installed according to the good Python,python3, you can enter PIP in cmd mode to see if it is installed)

1. Install the requests module with PIP

>>PIP Install Requests

Two, get request (no parameters)

1. After importing requests, the URL address can be accessed directly using the Get method, such as: Https://www.juhe.cn/loginStatus

2. Here R5 is response, the return value after the request, you can call response Status_code method to view the status code, headers view the returned header information, text is returned text information

Third, params

1. Send another GET request with parameters, such as in the blog Park Search: Bb,url Address: HTTP://ZZK.CNBLOGS.COM/S/BLOGPOST?KEYWORDS=BB

The first type: can be placed directly in the URL

The second type: by parameter

1. Request parameter: Keywords=yoyoketang, can be passed as a dictionary: {"Keywords": "BB"}

2. Multiple parameter formats: {"Key1": "Value1", "Key2": "value2", "Key3": "Value3"}

Iv. Content

1. Baidu Homepage If using R.text will find the content has garbled, because Baidu home response content is gzip compressed (non-text text)

2. If the Fiddler tool is garbled, it can be decoded after the click, in the code can be used r.content This method, content will automatically decode gzip and deflate compression

Wu, response

Return content for 1.response and more

--R.status_code #响应状态码
--R.content #字节方式的响应体, will automatically decode for you gzip and deflate compression
--R.headers #以字典对象存储服务器响应头, but this dictionary is very special, the dictionary key is not case-sensitive, if the key does not exist return none
--R.json () #Requests中内置的JSON解码器
--R.url # Get URL
--r.encoding # encoding format
--R.cookies # Get Cookie
--R.raw #返回原始响应体
--R.text #字符串方式的响应体, automatically decoded according to the character encoding of the response head
--R.raise_for_status () #失败请求 (not 200 responses) throws an exception

Python interface 1---send GET request

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.