Home > Article > Backend Development > How to install requests library in python
requests is a simple and easy-to-use HTTP library implemented in python. It is much simpler to use than urllib
Because it is a third-party library, cmd installation is required before use
pip install requests
After the installation is complete, import it. If it is normal, it means you can start using it.
Basic usage:
requests.get() is used to request the target website, the type is an HTTPresponse type
import requests response = requests.get('http://www.baidu.com') print(response.status_code) # 打印状态码 print(response.url) # 打印请求url print(response.headers) # 打印头信息 print(response.cookies) # 打印cookie信息print(response.text) #以文本形式打印网页源码 print(response.content) #以字节流形式打印
Running results:
Status code: 200
Various request methods:
import requests requests.get('http://httpbin.org/get') requests.post('http://httpbin.org/post') requests.put('http://httpbin.org/put') requests.delete('http://httpbin.org/delete') requests.head('http://httpbin.org/get') requests.options('http://httpbin.org/get')
Basic get request
import requests response = requests.get('http://httpbin.org/get')print(response.text)
GET request with parameters:
The first method is to directly The parameters are placed in the url
import requests response = requests.get(http://httpbin.org/get?name=gemey&age=22)print(response.text)
Parsing json
import requests response = requests.get('http://httpbin.org/get') print(response.text) print(response.json()) #response.json()方法同json.loads(response.text) print(type(response.json()))
The above is the detailed content of How to install requests library in python. For more information, please follow other related articles on the PHP Chinese website!