Home >Backend Development >Python Tutorial >How to install requests module in python

How to install requests module in python

silencement
silencementOriginal
2019-06-26 10:26:5110783browse

How to install requests module 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 module in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn