requests模块

- 基于如下5点展开requests模块的学习

  • 什么是requests模块

    • requests模块是python中原生的基于网络请求的模块,其主要作用是用来模拟浏览器发起请求。功能强大,用法简洁高效。在爬虫领域中占据着半壁江山的地位。
  • 为什么要使用requests模块
    • 因为在使用urllib模块的时候,会有诸多不便之处,总结如下:

      • 手动处理url编码
      • 手动处理post请求参数
      • 处理cookie和代理操作繁琐
      • ......
    • 使用requests模块:
      • 自动处理url编码
      • 自动处理post请求参数
      • 简化cookie和代理操作
      • ......
  • 如何使用requests模块
    • 安装:

      • pip install requests
    • 使用流程
      • 指定url
      • 基于requests模块发起请求
      • 获取响应对象中的数据值
      • 持久化存储
  • 通过5个基于requests模块的爬虫项目对该模块进行学习和巩固
    • 基于requests模块的get请求

      • 需求:爬取搜狗指定词条搜索后的页面数据
    • 基于requests模块的post请求
      • 需求:登录豆瓣电影,爬取登录成功后的页面数据
    • 基于requests模块ajax的get请求
    • 基于requests模块ajax的post请求
    • 综合练习
      • 需求:爬取搜狗知乎指定词条指定页码下的页面数据

- 代码展示

  • 需求:爬取搜狗指定词条搜索后的页面数据

    import requests
    import os
    #指定搜索关键字
    word = input('enter a word you want to search:')
    #自定义请求头信息
    headers={
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36',
    }
    #指定url
    url = 'https://www.sogou.com/web'
    #封装get请求参数
    prams = {
    'query':word,
    'ie':'utf-8'
    }
    #发起请求
    response = requests.get(url=url,params=param) #获取响应数据
    page_text = response.text with open('./sougou.html','w',encoding='utf-8') as fp:
    fp.write(page_text)
  • 需求:登录豆瓣电影,爬取登录成功后的页面数据
    import requests
    import os
    url = 'https://accounts.douban.com/login'
    #封装请求参数
    data = {
    "source": "movie",
    "redir": "https://movie.douban.com/",
    "form_email": "15027900535",
    "form_password": "bobo@15027900535",
    "login": "登录",
    }
    #自定义请求头信息
    headers={
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36',
    }
    response = requests.post(url=url,data=data)
    page_text = response.text with open('./douban111.html','w',encoding='utf-8') as fp:
    fp.write(page_text)
  • 需求:爬取豆瓣电影分类排行榜 https://movie.douban.com/中的电影详情数据
    #!/usr/bin/env python
    # -*- coding:utf-8 -*- import requests
    import urllib.request
    if __name__ == "__main__": #指定ajax-get请求的url(通过抓包进行获取)
    url = 'https://movie.douban.com/j/chart/top_list?' #定制请求头信息,相关的头信息必须封装在字典结构中
    headers = {
    #定制请求头中的User-Agent参数,当然也可以定制请求头中其他的参数
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36',
    } #定制get请求携带的参数(从抓包工具中获取)
    param = {
    'type':'5',
    'interval_id':'100:90',
    'action':'',
    'start':'0',
    'limit':'20'
    }
    #发起get请求,获取响应对象
    response = requests.get(url=url,headers=headers,params=param) #获取响应内容:响应内容为json串
    print(response.text)
  • 需求:爬取肯德基餐厅查询http://www.kfc.com.cn/kfccda/index.aspx中指定地点的餐厅数据
    #!/usr/bin/env python
    # -*- coding:utf-8 -*- import requests
    import urllib.request
    if __name__ == "__main__": #指定ajax-post请求的url(通过抓包进行获取)
    url = 'http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=keyword' #定制请求头信息,相关的头信息必须封装在字典结构中
    headers = {
    #定制请求头中的User-Agent参数,当然也可以定制请求头中其他的参数
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36',
    } #定制post请求携带的参数(从抓包工具中获取)
    data = {
    'cname':'',
    'pid':'',
    'keyword':'北京',
    'pageIndex': '1',
    'pageSize': '10'
    }
    #发起post请求,获取响应对象
    response = requests.get(url=url,headers=headers,data=data) #获取响应内容:响应内容为json串
    print(response.text)
  • 需求:爬取搜狗知乎指定词条指定页码下的页面数据
    import requests
    import os
    #指定搜索关键字
    word = input('enter a word you want to search:')
    #指定起始页码
    start_page = int(input('enter start page num:'))
    end_page = int(input('enter end page num:'))
    #自定义请求头信息
    headers={
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36',
    }
    #指定url
    url = 'https://zhihu.sogou.com/zhihu'
    #创建文件夹
    if not os.path.exists('./sougou'):
    os.mkdir('./sougou')
    for page in range(start_page,end_page+1):
    #封装get请求参数
    params = {
    'query':word,
    'ie':'utf-8',
    'page':str(page)
    }
    #发起post请求,获取响应对象
    response = requests.get(url=url,params=params)
    #获取页面数据
    page_text = response.text
    fileName = word+'_'+str(page)+'.html'
    filePath = './sougou/'+fileName
    with open(filePath,'w',encoding='utf-8') as fp:
    fp.write(page_text)
    print('爬取'+str(page)+'页结束')

爬虫概念 requests模块的更多相关文章

  1. 孤荷凌寒自学python第六十七天初步了解Python爬虫初识requests模块

    孤荷凌寒自学python第六十七天初步了解Python爬虫初识requests模块 (完整学习过程屏幕记录视频地址在文末) 从今天起开始正式学习Python的爬虫. 今天已经初步了解了两个主要的模块: ...

  2. Python爬虫练习(requests模块)

    Python爬虫练习(requests模块) 关注公众号"轻松学编程"了解更多. 一.使用正则表达式解析页面和提取数据 1.爬取动态数据(js格式) 爬取http://fund.e ...

  3. 06.Python网络爬虫之requests模块(2)

    今日内容 session处理cookie proxies参数设置请求代理ip 基于线程池的数据爬取 知识点回顾 xpath的解析流程 bs4的解析流程 常用xpath表达式 常用bs4解析方法 引入 ...

  4. Python网络爬虫之requests模块(2)

    session处理cookie proxies参数设置请求代理ip 基于线程池的数据爬取 xpath的解析流程 bs4的解析流程 常用xpath表达式 常用bs4解析方法 引入 有些时候,我们在使用爬 ...

  5. Python网络爬虫之requests模块

    今日内容 session处理cookie proxies参数设置请求代理ip 基于线程池的数据爬取 知识点回顾 xpath的解析流程 bs4的解析流程 常用xpath表达式 常用bs4解析方法 引入 ...

  6. Python爬虫之requests模块(2)

    一.今日内容 session处理cookie proxies参数设置请求代理ip 基于线程池的数据爬取 二.回顾 xpath的解析流程 bs4的解析流程 常用xpath表达式 常用bs4解析方法 三. ...

  7. 爬虫之requests模块的使用

    requests模块 概念:基于网络请求的模块 作用:用来模拟浏览器发请求,从而实现爬虫 环境安装:pip install requests 编码流程: 指定url 发起请求 获取响应数据 持久化存储 ...

  8. 爬虫之requests模块

    requests模块 什么是requests模块 requests模块是python中原生的基于网络请求的模块,其主要作用是用来模拟浏览器发起请求.功能强大,用法简洁高效.在爬虫领域中占据着半壁江山的 ...

  9. 04.Python网络爬虫之requests模块(1)

    引入 Requests 唯一的一个非转基因的 Python HTTP 库,人类可以安全享用. 警告:非专业使用其他 HTTP 库会导致危险的副作用,包括:安全缺陷症.冗余代码症.重新发明轮子症.啃文档 ...

随机推荐

  1. 获取laravel项目的路径的内置帮助函数

    app_path() app_path函数返回app目录的绝对路径: $path = app_path(); 你还可以使用app_path函数为相对于app目录的给定文件生成绝对路径: $path = ...

  2. CentOS7.x 配置开机启动项目

    Centos6.x 下版本主要通过service控制启动与关闭,通过chkconfig来设置开机启动项,但是Centos 7.x 版本采用已经Systemd来控制启动与关闭,Systemd 是 Lin ...

  3. bzoj 2806 [Ctsc2012]Cheat——广义后缀自动机+单调队列优化DP

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2806 只想着怎么用后缀数据结构做,其实应该考虑结合其他算法. 可以二分那个长度 L .设当前 ...

  4. EXPLAIN执行计划中要重点关注哪些要素

    MySQL的EXPLAIN当然和ORACLE的没法比,不过我们从它输出的结果中,也可以得到很多有用的信息. 总的来说,我们只需要关注结果中的几列: 列名 备注 type 本次查询表联接类型,从这里可以 ...

  5. C#发布程序添加其他程序文件

    注:程序发布文件,默认只发布自身程序直接引用的相关文件(A程序). 如果需要添加其他程序(不同的应用程序B)文件,操作方法如下: 第一步:将B程序文件复制到A程序 第二步:将B程序文件右键--> ...

  6. Windows Azure Virtual Machine (35) Azure VM通过Linked DB,执行SQL Job

    <Windows Azure Platform 系列文章目录> 今天遇到一个Case,客户在使用Azure Automation,执行Azure SQL Database 存储过程的时候, ...

  7. 我的Netty笔记

    pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w ...

  8. 三种通用应用层协议protobuf、thrift、avro对比,完爆xml,json,http

    原文: https://www.douban.com/note/523340109/ Google protobuf: 优点  二进制消息,性能好/效率高(空间和时间效率都很不错)     proto ...

  9. Java里数组的三种初始化方式

    静态初始化 除了用new关键字来产生数组以外,还可以直接在定义数组的同时就为数组元素分配空间并赋值. // 静态初始化 int[] iStaticArr = { 5, 2, 0 }; LOLHero[ ...

  10. [UE4]加入音效