Dimples's blog Dimples's blog
首页
  • 前端随笔

    • React
  • 系列文章

    • 《ES6 教程》笔记
    • 《Vue》笔记
    • 《TypeScript》笔记
  • 后端文章

    • Java
    • Python
    • 数据库
  • 学习笔记

    • 《Rust》笔记
    • 《SpringBoot》笔记
    • 《Django》笔记
    • 《Docker》笔记
    • 《Python爬虫》笔记
技术分享
友情链接
索引
关于
GitHub (opens new window)

DimplesY

不会写代码
首页
  • 前端随笔

    • React
  • 系列文章

    • 《ES6 教程》笔记
    • 《Vue》笔记
    • 《TypeScript》笔记
  • 后端文章

    • Java
    • Python
    • 数据库
  • 学习笔记

    • 《Rust》笔记
    • 《SpringBoot》笔记
    • 《Django》笔记
    • 《Docker》笔记
    • 《Python爬虫》笔记
技术分享
友情链接
索引
关于
GitHub (opens new window)
  • 爬虫介绍
  • 工具的使用
  • 爬取数据-urllib库
  • urllib库的高级用法
  • URLError与Cookie
  • Requests库的用法
    • 数据提取-正则表达式
    • 数据提取-Beautiful Soup
    • 数据提取-XPath
    • 数据提取-JsonPath
    • 数据提取-PyQuery
    • 爬虫之多线程
    • Selenium与PhantomJS
    • Selenium 处理滚动条
    • Python下Tesseract Ocr引擎及安装介绍
    • Scrapy 框架介绍与安装
    • 《Python爬虫》笔记
    DimplesY
    2022-03-06

    Requests库的用法

    # 1. 介绍

    对了解一些爬虫的基本理念,掌握爬虫爬取的流程有所帮助。入门之后,我们就需要学习一些更加高级的内容和工具来方便我们的爬取。那么这一节来简单介绍一下 requests 库的基本用法

    # 2. 安装

    利用 pip 安装

    pip install requests
    
    1

    # 3. 基本请求

    req = requests.get("http://www.baidu.com")
    req = requests.post("http://www.baidu.com")
    req = requests.put("http://www.baidu.com")
    req = requests.delete("http://www.baidu.com")
    req = requests.head("http://www.baidu.com")
    req = requests.options("http://www.baidu.com")
    
    1
    2
    3
    4
    5
    6

    # 3.1 get请求

    参数是字典,我们也可以传递json类型的参数:

    import requests
    
    url = "http://www.baidu.com/s"
    params = {'wd': '尚学堂'}
    response = requests.get(url, params=params)
    print(response.url)
    response.encoding = 'utf-8'
    html = response.text
    # print(html)
    
    1
    2
    3
    4
    5
    6
    7
    8
    9

    # 3.2 post请求

    参数是字典,我们也可以传递json类型的参数:

    url = "http://www.sxt.cn/index/login/login.html"
    formdata = {
        "user": "17703181473",
        "password": "123456"
    }
    response = requests.post(url, data=formdata)
    response.encoding = 'utf-8'
    html = response.text
    # print(html)
    
    1
    2
    3
    4
    5
    6
    7
    8
    9

    # 3.3 自定义请求头部

    伪装请求头部是采集时经常用的,我们可以用这个方法来隐藏:

    
    headers = {'User-Agent': 'python'}
    r = requests.get('http://www.zhidaow.com', headers = headers)
    print(r.request.headers['User-Agent'])
    
    1
    2
    3
    4

    # 3.4 设置超时时间

    可以通过timeout属性设置超时时间,一旦超过这个时间还没获得响应内容,就会提示错误

    requests.get('http://github.com', timeout=0.001)
    
    1

    # 3.5 代理访问

    采集时为避免被封IP,经常会使用代理。requests也有相应的proxies属性

    import requests
    
    proxies = {
      "http": "http://10.10.1.10:3128",
      "https": "https://10.10.1.10:1080",
    }
    
    requests.get("http://www.zhidaow.com", proxies=proxies)
    
    1
    2
    3
    4
    5
    6
    7
    8

    如果代理需要账户和密码,则需这样

    proxies = {
        "http": "http://user:pass@10.10.1.10:3128/",
    }
    
    1
    2
    3

    # 3.6 session自动保存cookies

    seesion的意思是保持一个会话,比如 登陆后继续操作(记录身份信息) 而requests是单次请求的请求,身份信息不会被记录

    # 创建一个session对象 
    s = requests.Session() 
    # 用session对象发出get请求,设置cookies 
    s.get('http://httpbin.org/cookies/set/sessioncookie/123456789') 
    
    1
    2
    3
    4

    # 3.7 ssl验证

    # 禁用安全请求警告
    requests.packages.urllib3.disable_warnings()
    
    resp = requests.get(url, verify=False, headers=headers)
    
    1
    2
    3
    4

    # 4 获取响应信息

    代码 含义
    resp.json() 获取响应内容(以json字符串)
    resp.text 获取响应内容 (以字符串)
    resp.content 获取响应内容(以字节的方式)
    resp.headers 获取响应头内容
    resp.url 获取访问地址
    resp.encoding 获取网页编码
    resp.request.headers 请求头内容
    resp.cookie 获取cookie
    帮助我修改此页面 (opens new window)
    #Python#爬虫
    上次更新: 2022/11/20, 18:28:09
    URLError与Cookie
    数据提取-正则表达式

    ← URLError与Cookie 数据提取-正则表达式→

    最近更新
    01
    使用 strapi 快速构建 API 和 CMS 管理系统
    03-03
    02
    Rust 开发环境
    11-26
    03
    使用 paka.dev 为 npm 包生成文档
    11-24
    更多文章>
    Theme by Vdoing | Copyright © 2020-2025 Dimples YJ | MIT License
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式