python的requests模块

官方文档地址:https://requests.readthedocs.io/projects/cn/zh-cn/latest/user/quickstart.html#id4

1、导入

import requests

2、发送http请求

# get请求
payload = {'key1': 'value1', 'key2': 'value2'}
url = "http://httpbin.org/get"
headers = {'user-agent': 'my-app/0.0.1'}
r = requests.get(url, headers=headers, params=payload)
r.text
r.encoding
r.content
r.json()


# post请求
import json
url = 'https://api.github.com/some/endpoint'
payload = {'key1': 'value1', 'key2': 'value2'}
r = requests.post(url, data = payload) 
# string 而不是一个 dict
r = requests.post(url, data=json.dumps(payload))
r.text


# post请求上传文件
url = 'http://httpbin.org/post'
files = {'file': open('report.xls', 'rb')}
r = requests.post(url, files=files)
r.text



# put请求
r = requests.put('http://httpbin.org/put', data = {'key':'value'})
# delete请求
r = requests.('http://httpbin.org/delete')

3、检测响应状态码

r.status_code

4、抛出异常

# 通过 Response.raise_for_status() 来抛出异常:
bad_r = requests.get('http://httpbin.org/status/404')
bad_r.status_code
bad_r.raise_for_status()

5、访问响应中的cookie

# 快速访问
url = 'http://example.com/some/cookie/setting/url'
r = requests.get(url)
r.cookies['example_cookie_name']

# 发送到服务器
url = 'http://httpbin.org/cookies'
cookies = dict(cookies_are='working')
r = requests.get(url, cookies=cookies)
r.text

6、重定向

# allow_redirects=False   禁止重定向
r = requests.get('http://github.com', allow_redirects=False)
r.history

7、响应时间

# timeout 仅对连接过程有效,与响应体的下载无关
requests.get('http://github.com', timeout=0.001)

8、Session

# 创建一个持久化的会话对象
r = requests.Session()
# 设置该会话的默认认证凭证,自动将用户名和密码编码为 Base64 格式,在后续所有请求的 Authorization 头中添加
r.auth = ('user', 'pass')

 

  

 

posted @ 2025-08-15 14:51  Alieen617  阅读(3)  评论(0)    收藏  举报