1. 基础 GET 请求

import requests

# 最基本的 GET 请求
url = "http://alwdqqo.haobachang2.loveli.com.cn:8888/flag"
response = requests.get(url)
print(response.text)

说明:

  • 使用 requests.get() 方法发送 GET 请求
  • 直接访问目标 URL 获取响应内容
  • 适用于不需要传递参数的简单接口

2. 基础 POST 请求(注释状态)

import requests

url = "http://8jmj5al.haobachang2.loveli.com.cn:8888/flag"
response = requests.post(url)
print(response.text)

说明:

  • 使用 requests.post() 方法发送 POST 请求
  • 当前代码被注释,如需使用请取消注释
  • 适用于需要 POST 方法的接口

3. POST 请求带表单数据

import requests

url = "http://agdsgwk.haobachang2.loveli.com.cn:8888/flag"
data = {'flag': '1'}  # 表单数据
response = requests.post(url, data=data)
print(response.text)

说明:

  • 使用 data 参数传递表单数据
  • 数据格式为字典 {'key': 'value'}
  • 适用于需要提交表单数据的接口

4. POST 请求带自定义请求头

import requests

url = "http://tw4nzsl.haobachang2.loveli.com.cn:8888/flag"
data = {"flag": "1"}  # 表单数据
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:143.0) Gecko/20100101 Firefox/143.0"
}
response = requests.post(url, data=data, headers=headers)
print(response.text)

说明:

  • 使用 headers 参数设置自定义请求头
  • 模拟浏览器 User-Agent 绕过简单反爬机制
  • 适用于需要特定请求头的接口

5. POST 请求带 JSON 数据

import requests

url = "http://wchhb7o.haobachang1.loveli.com.cn:8888/flag"
json_data = {"flag": "1"}  # JSON 数据
response = requests.post(url, json=json_data)
print(response.text)

说明:

  • 使用 json 参数直接传递 JSON 数据
  • requests 库会自动设置 Content-Type: application/json
  • 适用于需要 JSON 格式数据的 RESTful API

6. 使用技巧总结

6.1 请求方法选择

  • GET: 获取数据,参数在 URL 中
  • POST: 提交数据,参数在请求体中

6.2 参数传递方式

  1. URL 参数: requests.get(url, params={'key': 'value'})
  2. 表单数据: requests.post(url, data={'key': 'value'})
  3. JSON 数据: requests.post(url, json={'key': 'value'})

6.3 常见响应处理

# 获取响应状态码
status_code = request.status_code

# 获取响应头
headers = request.headers

# 获取 JSON 响应(如果响应是 JSON 格式)
json_data = request.json()

# 获取响应内容(文本格式)
text_content = request.text

# 获取响应内容(二进制格式)
binary_content = request.content

6.4 错误处理

try:
    response = requests.get(url, timeout=5)
    response.raise_for_status()  # 如果状态码不是 200,抛出异常
    print(response.text)
except requests.exceptions.Timeout:
    print("请求超时")
except requests.exceptions.HTTPError as err:
    print(f"HTTP 错误: {err}")
except requests.exceptions.RequestException as err:
    print(f"请求异常: {err}")

7. 扩展学习

7.1 会话保持

session = requests.Session()
session.get('http://example.com/login', params={'user': 'name', 'pass': 'word'})
response = session.get('http://example.com/dashboard')

原理说明:
使用普通的 requests.get()requests.post() 方法,每次请求都会建立新的连接,且不会携带之前请求的 Cookie。而 requests.Session() 对象能够实现会话保持。Session 对象内部维护了一个 CookieJar(Cookie 存储器)。当首次使用 session.get() 发起请求时,如果服务器返回了 Set-Cookie 响应头,这些 Cookie 会被自动存入 CookieJar 中。在后续向同一域名发起的请求中,Session 会自动从 CookieJar 中取出相应的 Cookie 并附加到请求头中。此外,Session 还会复用底层的 TCP 连接池,从而在保持会话状态(如登录态)的同时提升请求效率。

7.2 代理设置

proxies = {
    'http': 'http://10.10.1.10:3128',
    'https': 'http://10.10.1.10:1080',
}
requests.get('http://example.org', proxies=proxies)

7.3 文件上传

files = {'file': open('report.xls', 'rb')}
requests.post(url, files=files)

open(‘report.xls’, ‘rb’)
rb:二进制只读模式,以字节流读取 Excel 文件,上传文件必须用二进制打开,避免文本编码破坏二进制文件。
files 参数:
files = {“文件字段名”: (“文件名”, 文件字节流, 文件MIME类型)}


更多推荐