CrawlSpider类,Spider的一个子类
  - 全站数据爬取的方式
    - 基于Spider:手动请求
    - 基于CrawlSpider
  - CrawlSpider的使用:
    - 创建一个工程
    - cd XXX
  - 创建爬虫文件(CrawlSpider):
    - scrapy genspider -t crawl xxx www.xxxx.com
    - 链接提取器:
      - 作用:根据指定的规则(allow)进行指定链接的提取
    - 规则解析器:
      - 作用:将链接提取器提取到的链接进行指定规则(callback)的解析

示例:爬取sun网站中的编号,新闻标题,新闻内容,标号

分析:爬取的数据没有在同一张页面中。
  - 1.可以使用链接提取器提取所有的页码链接
  - 2.让链接提取器提取所有的新闻详情页的链接

爬虫文件

# -*- coding: utf-8 -*-
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
from Sun.items import SunItem,DetailItem class SunSpider(CrawlSpider):
name = 'sun'
# allowed_domains = ['www.xxx.com']
start_urls = ['http://wz.sun0769.com/political/index/politicsNewest?id=1&type=4']
# 链接提取器:根据指定规则进行指定链接的提取
link = LinkExtractor(allow=r'id=1&page=\d+')
link_detail = LinkExtractor(allow=r'index\?id=\d+') # 一个链接提取器对应一个规则解析器
rules = (
# 规则解析器:将链接提取器提取到的链接发送请求 并根据callback进行指定的解析操作
Rule(link, callback='parse_item', follow=False),
# follow=True 可以将链接提取器继续作用到 链接提取器提取的链接所对应的页面
# 通过此设置可对所有的页码进行爬取 调度器有去重过滤功能
Rule(link_detail, callback='parse_detail', follow=False),
) # 以下两个方法不可以进行请求传参
# 两个方法都把数据存储到item中 可采用两个item
def parse_item(self, response):
li_list = response.xpath('/html/body/div[2]/div[3]/ul[2]/li')
for li in li_list:
new_num = li.xpath('./span[1]/text()').extract_first()
title = li.xpath('./span[3]/a/text()').extract_first()
item = SunItem()
item['new_num'] = new_num
item['title'] = title
yield item def parse_detail(self, response):
print(111)
item = DetailItem()
new_id = response.xpath('/html/body/div[3]/div[2]/div[2]/div[1]/span[4]/text()').extract_first()
content = response.xpath('/html/body/div[3]/div[2]/div[2]/div[2]//text()').extract_first()
print(new_id, content)
item['new_id'] = new_id
item['content'] = content
yield item

items.py

class SunPipeline:

    def process_item(self, item, spider):

        if item.__class__.__name__ == 'SunItem':  # 获取当前的item的类名的字符串
new_num = item['new_num']
title = item['title']
else:
print(22)
new_id = item['new_id']
content = item['content']
print(new_id,content)
return item

注:在写正则表达式时,需要对特殊的符号进行转义。

Scrapy框架(八)--CrawlSpider的更多相关文章

  1. 爬虫scrapy框架之CrawlSpider

    爬虫scrapy框架之CrawlSpider   引入 提问:如果想要通过爬虫程序去爬取全站数据的话,有几种实现方法? 方法一:基于Scrapy框架中的Spider的递归爬取进行实现(Request模 ...

  2. Python网络爬虫之Scrapy框架(CrawlSpider)

    目录 Python网络爬虫之Scrapy框架(CrawlSpider) CrawlSpider使用 爬取糗事百科糗图板块的所有页码数据 Python网络爬虫之Scrapy框架(CrawlSpider) ...

  3. python爬虫入门(八)Scrapy框架之CrawlSpider类

    CrawlSpider类 通过下面的命令可以快速创建 CrawlSpider模板 的代码: scrapy genspider -t crawl tencent tencent.com CrawSpid ...

  4. Scrapy框架之CrawlSpider

    提问:如果想要通过爬虫程序去爬取”糗百“全站数据新闻数据的话,有几种实现方法? 方法一:基于Scrapy框架中的Spider的递归爬取进行实现(Request模块递归回调parse方法). 方法二:基 ...

  5. 16.Python网络爬虫之Scrapy框架(CrawlSpider)

    引入 提问:如果想要通过爬虫程序去爬取”糗百“全站数据新闻数据的话,有几种实现方法? 方法一:基于Scrapy框架中的Spider的递归爬取进行实现(Request模块递归回调parse方法). 方法 ...

  6. scrapy框架之CrawlSpider操作

    提问:如果想要通过爬虫程序去爬取”糗百“全站数据新闻数据的话,有几种实现方法? 方法一:基于Scrapy框架中的Spider的递归爬取进行实现(Request模块递归回调parse方法). 方法二:基 ...

  7. 爬虫开发11.scrapy框架之CrawlSpider操作

    提问:如果想要通过爬虫程序去爬取”糗百“全站数据新闻数据的话,有几种实现方法? 方法一:基于Scrapy框架中的Spider的递归爬取进行实现(Request模块递归回调parse方法). 方法二:基 ...

  8. scrapy框架基于CrawlSpider的全站数据爬取

    引入 提问:如果想要通过爬虫程序去爬取”糗百“全站数据新闻数据的话,有几种实现方法? 方法一:基于Scrapy框架中的Spider的递归爬取进行实现(Request模块递归回调parse方法). 方法 ...

  9. scrapy框架之(CrawlSpider)

    一.CrawlSpider简介 如果想要通过爬虫程序去爬取”糗百“全站数据新闻数据的话,有几种实现方法? 方法一:基于Scrapy框架中的Spider的递归爬取进行实现(Request模块递归回调pa ...

  10. 16,Python网络爬虫之Scrapy框架(CrawlSpider)

    今日概要 CrawlSpider简介 CrawlSpider使用 基于CrawlSpider爬虫文件的创建 链接提取器 规则解析器 引入 提问:如果想要通过爬虫程序去爬取”糗百“全站数据新闻数据的话, ...

随机推荐

  1. HarmonyOS NEXT应用开发—自定义视图实现Tab效果

    介绍 本示例介绍使用Text.List等组件,添加点击事件onclick,动画,animationTo实现自定义Tab效果. 效果预览图 使用说明 点击页签进行切换,选中态页签字体放大加粗,颜色由灰变 ...

  2. 日志服务SLS 助力识货 APP,解决业务数据采集查询监控问题

    简介: 日志服务SLS 助力识货 APP,解决业务数据采集查询监控问题 更多存储标杆案例欢迎点击下方链接查看 阿里云存储标杆案例样板间 公司介绍识货APP是虎扑体育旗下的导购应用,致力于为广大年轻用户 ...

  3. dubbo-go v3 版本 go module 踩坑记

    简介: 该问题源于我们想对 dubbo-go 的 module path 做一次变更,使用 dubbo.apache.org/dubbo-go/v3 替换之前的 github.com/apache/d ...

  4. 汽车之家:基于 Flink + Iceberg 的湖仓一体架构实践

    简介: 由汽车之家实时计算平台负责人邸星星在 4 月 17 日上海站 Meetup 分享的,基于 Flink + Iceberg 的湖仓一体架构实践. 内容简要: 一.数据仓库架构升级的背景 二.基于 ...

  5. jqGrid--设置单元格字体颜色

    colModel: [ { name: '列名称', index: '列名称', width: 65, sortable: true, resizable: false, cellattr: addC ...

  6. k8s证书延长时间(二)

    1.查看证书有效时间 # 通过下面可看到ca证书有效期是10年,2022-2032 [root@master ~]# openssl x509 -in /etc/kubernetes/pki/ca.c ...

  7. C 语言编程 — 高级数据类型 — 指针

    目录 文章目录 目录 前文列表 指针 声明指针 使用指针 NULL 指针 指针的算术运算 指向指针的指针 将指针作为实际参数传入函数 从函数返回指针 一个古老的笑话 前文列表 <程序编译流程与 ...

  8. 国产系统UOS安装体验

    原文链接 https://www.giantliu.cn/2020/09/04/200904InstallUOS/ UOS简介 统信桌面操作系统(Uniontech OS)个人正式版是统信软件基于Li ...

  9. 安利一个好用的IDEA插件 object-helper-plugin

    更多精彩博文请关注:听到微笑的博客 一. 插件背景 object-helper 插件是一个日常开发工具集插件,提供丰富的功能,最开始是基于 GenerateO2O 插件开发而来,它提供了对象之间值拷贝 ...

  10. TS码流解析(二)PSI PAT PMT

    TS码流有PSI和PES两种负载,这一节主要来了解PSI是如何解析的. 1.PSI PSI(Program Specific Information)节目专用信息,用来描述TS码流的节目组成等信息.P ...