How to install requests library in python

silencement
Release: 2019-06-25 14:08:37
Original
12995 people have browsed it

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
Copy after login

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) #以字节流形式打印
Copy after login

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')
Copy after login

Basic get request

import requests

response = requests.get('http://httpbin.org/get')print(response.text)
Copy after login

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)
Copy after login

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()))
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template