requests

如何使用requests

官网文档,已经写的足够好了,就不重复了。详见:

此处仅仅(摘录)列举最简单的内容,供了解:

安装

pip install requests

基本用法举例

导入,get请求:

import requests

r = requests.get('https://api.github.com/events')

POST带参数,内部会自动把dict转换成json

r = requests.post('https://httpbin.org/post', data = {'key':'value'})

GET带参数,内部自动转换dictkey=valuequery parameter

payload = {'key1': 'value1', 'key2': 'value2'}
r = requests.get('https://httpbin.org/get', params=payload)

返回的response:

r = requests.get('https://api.github.com/events')
r.text
# '[{"repository":{"open_issues":0,"url":"https://github.com/...

response有各种属性可用:

查看字符编码:

r.encoding
# 'utf-8'

手动设置编码:

r.encoding = 'ISO-8859-1'

返回内容:

r.content
# b'[{"repository":{"open_issues":0,"url":"https://github.com/...

等等。

results matching ""

    No results matching ""