一.CrawlSpider简介

如果想要通过爬虫程序去爬取”糗百“全站数据新闻数据的话,有几种实现方法?

方法一:基于Scrapy框架中的Spider的递归爬取进行实现(Request模块递归回调parse方法)。

方法二:基于CrawlSpider的自动爬取进行实现(更加简洁和高效)。

一.简介


  CrawlSpider其实是Spider的一个子类,除了继承到Spider的特性和功能外,还派生除了其自己独有的更加强大的特性和功能。其中最显著的功能就是”LinkExtractors链接提取器“。Spider是所有爬虫的基类,其设计原则只是为了爬取start_url列表中网页,而从爬取到的网页中提取出的url进行继续的爬取工作使用CrawlSpider更合适。


二.使用


  1.创建scrapy工程:scrapy startproject projectName


  2.创建爬虫文件:scrapy genspider -t crawl spiderName www.xxx.com


    --此指令对比以前的指令多了 "-t crawl",表示创建的爬虫文件是基于CrawlSpider这个类的,而不再是Spider这个基类。


  3.观察生成的爬虫文件

  爬虫文件.py

# -*- coding: utf-8 -*-
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
#不再是引入spider,而是引入了crawlspider,还引入了LinkExtracor(连接提取器),Rule解析器 class ChoutiSpider(CrawlSpider):
name = 'chouti'
#allowed_domains = ['www.xxx.com']
start_urls = ['https://dig.chouti.com/r/scoff/hot/1']
  #allow后面跟着正则匹配,用正则去匹配符合的连接
  #rule规则解析器则会去把提取器提取到的连接发起请求,并把获得的响应对象用回调函数去解析
  #follow表示是否把连接解析器继续作用到提取到的url中(是否提取全站的url)
  #这是一个元组
rules = (
Rule(LinkExtractor(allow=r'Items/'), callback='parse_item', follow=True),
) def parse_item(self, response):
item = {}
#item['domain_id'] = response.xpath('//input[@id="sid"]/@value').get()
#item['name'] = response.xpath('//div[@id="name"]').get()
#item['description'] = response.xpath('//div[@id="description"]').get()
return item

  案例一:(全站提取)

# -*- coding: utf-8 -*-
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule class ChoutiSpider(CrawlSpider):
name = 'chouti'
# allowed_domains = ['www.xxx.com']
start_urls = ['https://dig.chouti.com/r/scoff/hot/1']
#把这个单独写比较好看
link=LinkExtractor(allow=r'/r/scoff/hot/\d+')
rules = (
Rule(link,callback='parse_item', follow=True),
) def parse_item(self, response):
print(response) #这样就可以迭代提取到我们想要的所有内容,因为其起始页的url为:https://dig.chouti.com/r/scoff/hot/1

  案例二:(第一页没有数字编号的)

class ChoutiSpider(CrawlSpider):
name = 'chouti'
# allowed_domains = ['www.xxx.com']
start_urls = ['https://www.qiushibaike.com/text/']
#把这个单独写比较好看 link=LinkExtractor(allow=r'/text/page/\d+/')
link1=LinkExtractor(allow=r'/text/')
rules = (
Rule(link,callback='parse_item', follow=True),
Rule(link1, callback='parse_item', follow=True),
) def parse_item(self, response):
print(response)

#注意观察器其实url:
https://www.qiushibaike.com/text/
#第一页没有数字表示

  案例三:(正匹配会有很多相似的,限定开头或者结尾)

class ChoutiSpider(CrawlSpider):
name = 'chouti'
# allowed_domains = ['www.xxx.com']
start_urls = ['https://www.qiushibaike.com/pic/']
# 把这个单独写比较好看

  #这边的?记得转义\  
link = LinkExtractor(allow=r'/pic/page/\d+\?s=')
link1 = LinkExtractor(allow=r'/pic/$') #提取第一页这个匹配会有很多其他的干扰,这些并不是我们想要的,要限定结尾$
rules = (
Rule(link, callback='parse_item', follow=True),
Rule(link1, callback='parse_item', follow=True),
) def parse_item(self, response):
print(response)

  注:如果allow没有为空,那就是匹配网页中所有的url

scrapy框架之(CrawlSpider)的更多相关文章

  1. 爬虫scrapy框架之CrawlSpider

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

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

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

  3. Scrapy框架之CrawlSpider

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

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

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

  5. scrapy框架之CrawlSpider操作

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

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

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

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

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

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

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

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

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

随机推荐

  1. 16.数据类型(data_type)

    CREATE TABLE 语句 CREATE TABLE 语句用于创建数据库中的表. SQL CREATE TABLE 语法 CREATE TABLE 表名称 ( 列名称1 数据类型, 列名称2 数据 ...

  2. pyinstaller生成exe可执行程序

    1安装 略 2执行 pyinstaller –F test.py 报错: Pyinstaller: cx_Oracle.InterfaceError: Unable to acquire Oracle ...

  3. C#使用var定义变量时的四个特点

    使用var定义变量时有以下四个特点: 1. 必须在定义时初始化.也就是必须是var s = “abcd”形式: 2. 一但初始化完成,就不能再给变量赋与初始化值类型不同的值了. 3.   var要求是 ...

  4. 凑算式——第七届蓝桥杯C语言B组(省赛)第三题

    原创 凑算式 B      DEFA + --- + ------- = 10       C      GHI (如果显示有问题,可以参见[图1.jpg]) 这个算式中A~I代表1~9的数字,不同的 ...

  5. java代码实现顺序队列

    java实现顺序队列 package xianxinTable; import java.util.ArrayList; import java.util.Iterator; import com.s ...

  6. ComicEnhancerPro 系列教程十九:用JpegQuality看JPG文件的压缩参数

    作者:马健邮箱:stronghorse_mj@hotmail.com 主页:http://www.comicer.com/stronghorse/ 发布:2017.07.23 教程十九:用JpegQu ...

  7. Highsoft.Highcharts 5.0.6439.38401 key

    Highcharts .NET allows developers to make charts using Highcharts API with the Microsoft .NET Framew ...

  8. 指针和动态分配内存 (不定长度数组)------新标准c++程序设计

    背景: 数组的长度是定义好的,在整个程序中固定不变.c++不允许定义元素个数不确定的数组.例如: int n; int a[n]; //这种定义是不允许的 但是在实际编程中,往往会出现要处理的数据数量 ...

  9. windows下安装newman

    1.下载安装node.js,下载地址::https://nodejs.org/en/download/,这里我下载的为v10.15.0-x64.msi,下载后直接安装即可,安装完后可输入node -v ...

  10. 题解 P1720 【月落乌啼算钱】

    题目链接 定义一个函数比较好求. #include<bits/stdc++.h>//万能头文件 using namespace std; double F(int x)//定义函数,为了保 ...