06. Requests库的用法

06. Requests库的用法
最新回答
四叶草紫丁香

2020-06-12 16:51:15

Requests库是Python中用于发送HTTP请求的第三方库,因其简洁易用的API设计,成为网络爬虫和API交互的首选工具。以下是对其核心用法的系统总结:

一、基础安装与请求方法
  1. 安装通过pip命令快速安装:

    pip install requests
  2. 请求方法支持所有HTTP动词,示例如下:

    import requests# GET请求req = requests.get("
    http://www.baidu.com"
    )# POST请求req = requests.post("
    http://www.baidu.com"
    )# 其他方法(PUT/DELETE/HEAD/OPTIONS)
二、请求参数处理
  1. GET请求传参通过params传递字典或JSON参数,自动编码URL:

    url = "
    http://www.baidu.com/s"params
    = {'wd': '尚学堂'}response = requests.get(url, params=params)print(response.url) # 输出编码后的URL
  2. POST请求传参使用data传递表单数据,或json直接发送JSON:

    url = "
    http://www.sxt.cn/index/login/login.html"formdata
    = {"user": "17703181473", "password": "123456"}response = requests.post(url, data=formdata)
三、高级功能配置
  1. 自定义请求头伪装浏览器访问,避免被识别为爬虫:

    headers = {'User-Agent': 'Mozilla/5.0'}r = requests.get('
    http://www.zhidaow.com'
    , headers=headers)
  2. 超时设置防止长时间等待无响应:

    requests.get('
    http://github.com'
    , timeout=5) # 5秒超时
  3. 代理配置支持HTTP/HTTPS代理,可添加认证信息:

    proxies = { "http": "
    http://10.10.1.10:3128"
    , "https": "
    http://user:pass@10.10.1.10:3128"
    }requests.get("
    http://www.zhidaow.com"
    , proxies=proxies)
  4. 会话保持自动管理Cookies,适用于登录后操作:

    s = requests.Session()s.get('
    http://httpbin.org/cookies/set/sessioncookie/123456789'
    )
  5. SSL证书验证禁用证书验证(不推荐生产环境使用):

    requests.packages.urllib3.disable_warnings()resp = requests.get(url, verify=False)
四、响应信息处理
  1. 内容获取

    resp.text:解码后的字符串(自动处理编码)

    resp.content:原始字节数据(适合非文本内容)

    resp.json():解析JSON响应(需响应体为合法JSON)

  2. 元信息提取

    resp.headers:响应头字典

    resp.url:最终请求URL(含重定向)

    resp.cookies:服务器设置的Cookies

    resp.request.headers:实际发送的请求头

五、典型应用场景
  1. 网页爬取

    response = requests.get("
    https://example.com"
    )response.encoding = 'utf-8' # 手动指定编码print(response.text)
  2. API交互

    data = {'key': 'value'}response = requests.post("
    https://api.example.com"
    , json=data)print(response.json())
  3. 文件下载

    r = requests.get("
    https://example.com/file.pdf"
    , stream=True)with open("file.pdf", "wb") as f: f.write(r.content)
六、注意事项
  1. 始终检查响应状态码:resp.status_code
  2. 处理异常:使用try-except捕获requests.exceptions.RequestException
  3. 遵守robots.txt规则,合理设置请求间隔(如time.sleep(1))

Requests库通过简洁的接口封装了复杂HTTP操作,结合其丰富的功能(如会话保持、代理支持),能够高效应对大多数网络请求场景。建议结合官方文档深入学习高级特性(如文件上传、OAuth认证等)。