别院牧志知识库 别院牧志知识库
首页
  • 基础

    • 全栈之路
    • 😎Awesome资源
  • 进阶

    • Python 工匠系列
    • 高阶知识点
  • 指南教程

    • Socket 编程
    • 异步编程
    • PEP 系列
  • 面试

    • Python 面试题
    • 2025 面试记录
    • 2022 面试记录
    • 2021 面试记录
    • 2020 面试记录
    • 2019 面试记录
    • 数据库索引原理
  • 基金

    • 基金知识
    • 基金经理
  • 细读经典

    • 德隆-三个知道
    • 孔曼子-摊大饼理论
    • 配置者说-躺赢之路
    • 资水-建立自己的投资体系
    • 反脆弱
  • Git 参考手册
  • 提问的智慧
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
首页
  • 基础

    • 全栈之路
    • 😎Awesome资源
  • 进阶

    • Python 工匠系列
    • 高阶知识点
  • 指南教程

    • Socket 编程
    • 异步编程
    • PEP 系列
  • 面试

    • Python 面试题
    • 2025 面试记录
    • 2022 面试记录
    • 2021 面试记录
    • 2020 面试记录
    • 2019 面试记录
    • 数据库索引原理
  • 基金

    • 基金知识
    • 基金经理
  • 细读经典

    • 德隆-三个知道
    • 孔曼子-摊大饼理论
    • 配置者说-躺赢之路
    • 资水-建立自己的投资体系
    • 反脆弱
  • Git 参考手册
  • 提问的智慧
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • 辨析

  • Sockets编程

  • Django

  • stackoverflow

  • Flask

  • 全栈之路

    • 基础篇

    • 进阶篇

    • 面向对象

    • 网络编程

    • 操作系统

    • 标准库

    • 第三方库

      • Python 标准库系列之 Memcache 模块
      • Python 标准库系列之 MySQLdb 模块
      • Python 标准库系列之 Paramiko 模块
      • Python 全栈之路系列之 RabbitMQ
      • Python 标准库系列之 Redis 模块
      • Python 标准库系列之 requests 模块
        • 安装Requests模块
        • 环境准备
        • 简单的一个 requests 小程序
        • 发送请求
        • 实例
      • Python 全栈之路系列之 scrapy 爬虫
      • Python 全栈之路系列之 SQLAlchemy
      • Python 中更优雅的日志记录方案 loguru
    • Python 全栈之路
  • 面试

  • 代码片段

  • 异步编程

  • 😎Awesome资源

  • PEP

  • Python工匠系列

  • 高阶知识点

  • Python 学习资源待整理
  • 设计模式

  • 好“艹蛋”的 Python 呀!
  • FIFO | 待学清单📝
  • pip 安装及使用
  • 数据分析

  • 源码阅读计划

  • OOP

  • 关于 python 中的 setup.py
  • 并行分布式框架 Celery
  • 七种武器,让你的代码提高可维护性
  • 使用 pdb 调试 Python 代码
  • 每周一个 Python 标准库
  • 🐍Python
  • 全栈之路
  • 第三方库
佚名
2020-05-23
目录

Python 标准库系列之 requests 模块

# Python 标准库系列之 requests 模块

Requests is the only Non-GMO HTTP library for Python, safe for human consumption.

官方文档:http://docs.python-requests.org/en/master/

# 安装Requests模块

Requests模块官方提供了两种方式安装:

pip 方式安装

pip install requests
1

源码方式安装

git clone git://github.com/kennethreitz/requests.git
cd requests
python setup.py install
1
2
3

验证是否安装成功

进入python解释的,导入模块试试,如果导入成功则安装成功,否则就需要检查那里执行错误了呢。

C:\Users\anshengme> python
Python 3.5.1 (v3.5.1:37a07cee5969, Dec  6 2016, 01:54:25) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
1
2
3
4

# 环境准备

安装gunicorn和httpbin

sudo pip3 install gunicorn httpbin
1

启动一个gunicornServer

⇒  sudo gunicorn httpbin:app
[2016-10-27 11:45:08 +0800] [12175] [INFO] Starting gunicorn 19.6.0
[2016-10-27 11:45:08 +0800] [12175] [INFO] Listening at: https://blog.ansheng.me:8000 (12175)
[2016-10-27 11:45:08 +0800] [12175] [INFO] Using worker: sync
[2016-10-27 11:45:08 +0800] [12178] [INFO] Booting worker with pid: 12178
1
2
3
4
5

打开浏览器输入将会得到以下页面,相当于在本地启动一个 http server 便于学习 requests 模块

requests-01

# 简单的一个 requests 小程序

下面的一个小程序会通过requests请求'https://blog.ansheng.me:8000/ip'这个 URI 链接,获取到对应的数据。

#!/use/bin/env python3
# _*_ coding:utf-8 _*_
import requests

URL_IP = 'https://blog.ansheng.me:8000/ip'

def use_params_requests():
    # 参数
    params = {'params1': 'Hello', 'params2': 'World'}
    # 发送请求
    response = requests.get(URL_IP, params=params)
    print("响应的状态码:", response.status_code, response.reason)
    print("返回的头部:", response.headers)
    print("把返回的数据转换为json:", response.json())
    print("响应的文本:", response.text)

if __name__ == '__main__':
    use_params_requests()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

# 发送请求

通过 GITHUB 提供的 API 获取用户信息

#!/use/bin/env python3
# _*_ coding:utf-8 _*_
import requests
import json

URL = 'https://api.github.com'


def build_uri(endpoint):
    return '/'.join([URL, endpoint])


def better_print(json_str):
    return json.dumps(json.loads(json_str), indent=4)


def request_method():
    # 获取用户的所有信息
    response = requests.get(build_uri('users/anshengme'))
    print(better_print((response.text)))

    print("\n")

    # 获取用户的邮箱
    response = requests.get(build_uri('user/emails'), auth=("anshengme.com@gmail.com", "xxxxxx"))
    print(better_print((response.text)))


if __name__ == '__main__':
    request_method()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

带参数的请求

# 使用params传参
def params_request():
    response = requests.get(build_uri('users'), params={'since': 11})
    print(better_print(response.text))
    print(response.request.headers)
    print(response.url)

# 使用json传参方式
def json_request():
    response = requests.get(build_uri('user'), auth=("username", "email"), json={"name": "asdas"})
    print(better_print(response.text))
1
2
3
4
5
6
7
8
9
10
11

异常处理

def timeout_request():
    try:
        response = requests.get(build_uri('user/emails'), timeout=10)
        response.raise_for_status()
    except Exception as e:
        print(e)
    else:
        print(response.text)
        print(response.status_code)
1
2
3
4
5
6
7
8
9

自定义 request

from requests import Request, Session

def hard_request():
    s = Session()
    # 创建请求
    headers = {'User-Agent': 'fake1.3.4'}
    req = Request('GET', build_uri('user/emails'), auth=('anshengme.com@gmail.com', 'xxxxxx'), headers=headers)
    prepped = req.prepare()
    print("请求头》》", prepped.headers)
    # 发送请求
    resp = s.send(prepped)
    print(resp.status_code)
    print(resp.request.headers)
    print(resp.text)
1
2
3
4
5
6
7
8
9
10
11
12
13
14

# 实例

下载图片/文件

#!/use/bin/env python3
# _*_ coding:utf-8 _*_
import requests
from contextlib import closing


# 流传输的模式
def download_img():
    url = "http://www.sinaimg.cn/IT/cr/2016/0331/725124517.jpg"
    # headers = {
    #     'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36'}
    # response = requests.get(url, headers=headers, stream=True)
    response = requests.get(url, stream=True)
    print(response.status_code, response.reason)

    with open('github.jpg', 'wb') as fd:
        for chunk in response.iter_content(128):
            fd.write(chunk)

def download_img_improved():
    url = "http://www.sinaimg.cn/IT/cr/2016/0331/725124517.jpg"
    with closing(requests.get(url, stream=True)) as response:
        # 打开并写入文件
        with open('github1.jpg', 'wb') as fd:
            for chunk in response.iter_content(128):
                fd.write(chunk)

download_img()
download_img_improved()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

处理响应的事件钩子

#!/use/bin/env python3
# _*_ coding:utf-8 _*_
import requests

def get_key_info(response, *args, **kwargs):
    print(response.headers["Content-Type"])

def main():
    requests.get("https://www.baidu.com", hooks=dict(response=get_key_info))

if __name__ == "__main__":
    main()
1
2
3
4
5
6
7
8
9
10
11
12
编辑 (opens new window)
#编码#面向对象
上次更新: 2024-07-23, 01:00:43
Python 标准库系列之 Redis 模块
Python 全栈之路系列之 scrapy 爬虫

← Python 标准库系列之 Redis 模块 Python 全栈之路系列之 scrapy 爬虫→

最近更新
01
2025 面试记录
05-28
02
提升沟通亲和力的实用策略
03-26
03
工作
07-15
更多文章>
Theme by Vdoing | Copyright © 2019-2025 IMOYAO | 别院牧志
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式