scrapy框架之(CrawlSpider)

一.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

 

    样例:抓取博客园

复制代码
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
from tengxunzhaopin.items import BokeyuanItem


class BokeyuanSpider(CrawlSpider):
    name = 'bokeyuan'
    allowed_domains = ['www.cnblogs.com']
    start_urls = ['https://www.cnblogs.com/']

    rules = (
        Rule(LinkExtractor(allow=r'/sitehome/p/\d+'), callback='parse_item', follow=True),
    )

    def parse_item(self, response):
        item = BokeyuanItem()
        a_list = response.xpath('//a[@class="post-item-title"]')

        for aa in a_list:
            href = aa.xpath("./@href").get()
            title = aa.xpath("./text()").get()
            item["title"] = title
            item["href"] = href
            yield item
        # return item
复制代码

 

posted @   阿布_alone  阅读(226)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
TOP
点击右上角即可分享
微信分享提示