- Requests 教程
- Requests - 首页
- Requests - 概述
- Requests - 环境设置
- Requests - Http 请求的工作原理?
- Requests - 使用 Requests
- 处理 HTTP 请求的响应
- Requests - HTTP 请求头
- Requests - 处理 GET 请求
- 处理 POST、PUT、PATCH 和 DELETE 请求
- Requests - 文件上传
- Requests - 使用 Cookie
- Requests - 处理错误
- Requests - 处理超时
- Requests - 处理重定向
- Requests - 处理历史记录
- Requests - 处理会话
- Requests - SSL 证书
- Requests - 身份验证
- Requests - 事件挂钩
- Requests - 代理
- Requests - 使用 Requests 进行网络抓取
- Requests 实用资源
- Requests - 快速指南
- Requests - 实用资源
- Requests - 讨论
Requests - 处理 GET 请求
本章将集中介绍 GET 请求,该请求最为常用,并且经常使用。requests 模块中 GET 的工作方式非常简单。以下是一个使用 GET 方法处理 URL 的简单示例。
示例
import requests getdata = requests.get('https://jsonplaceholder.typicode.com/users') print(getdata.content) getdata.content, will print all the data available in the response.
输出
E:\prequests>python makeRequest.py b'[\n {\n "id": 1,\n "name": "Leanne Graham",\n "username": "Bret",\n "email": "[email protected]",\n "address": {\n "street": "Kulas Light ",\n "suite": "Apt. 556",\n "city": "Gwenborough",\n "zipcode": " 92998-3874",\n "geo": {\n "lat": "-37.3159",\n "lng": "81.149 6"\n }\n },\n "phone": "1-770-736-8031 x56442",\n "website": "hildegard.org",\n "company": {\n "name": "Romaguera-Crona",\n "catchPhrase": "Multi-layered client-server neural-net",\n "bs": "harness real-time e-markets"\n }\n }
还可以使用 param 属性将参数传递给 get 方法,如下所示 −
import requests payload = {'id': 9, 'username': 'Delphine'} getdata = requests.get('https://jsonplaceholder.typicode.com/users', params=payload) print(getdata.content)
详细信息存储在键/值对中的对象 payload 中,并传递给 get() 方法内的 params。
输出
E:\prequests>python makeRequest.py b'[\n {\n "id": 9,\n "name": "Glenna Reichert",\n "username": "Delphine", \n "email": "[email protected]",\n "address": {\n "street": "Dayna Park",\n "suite": "Suite 449",\n "city": "Bartholomebury",\n "zipcode": "76495-3109",\n "geo": {\n "lat": "24.6463",\n "lng": "-168.8889"\n }\n },\n "phone": "(775)976-6794 x41206",\n " website": "conrad.com",\n "company": {\n "name": "Yost and Sons",\n "catchPhrase": "Switchable contextually-based project",\n "bs": "aggregate real-time technologies"\n }\n }\n]'
广告