Python interface automation 1-Send GET Request "reprint"

Source: Internet
Author: User

From the blog: Shanghai-leisurely

Original address: http://www.cnblogs.com/yoyoketang/tag/python%E6%8E%A5%E5%8F%A3%E8%87%AA%E5%8A%A8%E5%8C%96/

Objective

Requests module, that is, old dirty turtle, why call it old dirty turtle, because this official online logo is this dirty turtle, the next is to learn it.

First, environmental installation

1. Install the requests module with PIP

>>PIP Install Requests

Second, GET request

1. After importing requests, the URL address can be accessed directly using the Get method, such as: http://www.cnblogs.com/yoyoketang/, it looks cool.

2. Here r is response, the return value after the request, you can call response in the Status_code method to view the status code

3. Status code 200 can only indicate that the server address accessed by this interface is right, and does not explain the function OK, generally to see the content of the response, R.text is to return text information

Third, params

1. Send another GET request with parameters, such as in the blog Park Search: Yoyoketang,url Address: Http://zzk.cnblogs.com/s/blogpost?Keywords=yoyoketang

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

3. 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 automation 1-Send GET Request "reprint"

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.