pip install requests
如果较慢可以用使用
1.镜像云安装
2.使用pycharm安装
import requests 导入
GET请求:r = requests.get(‘https://api.github.com/events’)
POST请求:r = requests.post(‘https://httpbin.org/post’, data = {‘key’:‘value’})
如何携带请求参数?
payload = {‘key1’: ‘value1’, ‘key2’: ‘value2’}
r = requests.get(‘https://httpbin.org/get’, params=payload)
带上浏览器头
url = ‘https://api.github.com/some/endpoint’
headers = {‘user-agent’: ‘自己填写’}
r = requests.get(url, headers=headers)
获取服务器响应文本内容
import requests
r = requests.get(‘https://api.github.com/events’)
r.text
r.encoding
r.content
获取响应码
r = requests.get(‘https://httpbin.org/get’)
r.status_code
获取响应头
r.headers
获取 Json 响应内容
import requests
r = requests.get(‘https://api.github.com/events’)
r.json()
请求的时候用 json 作为参数
url = ‘https://api.github.com/some/endpoint’
payload = {‘some’: ‘data’}
r = requests.post(url, json=payload)
python代码转换网站:https://curl.trillworks.com/
2021最新《python爬虫从0-1》
教程地址:https://www.bilibili.com/video/BV1gK4y1R7Ha