python网络爬虫 CrawlSpider使用详解

yipeiwu_com6年前Python爬虫

CrawlSpider

  • 作用:用于进行全站数据爬取
  • CrawlSpider就是Spider的一个子类
  • 如何新建一个基于CrawlSpider的爬虫文件
    • scrapy genspider -t crawl xxx www.xxx.com
  • 例:choutiPro

LinkExtractor连接提取器:根据指定规则(正则)进行连接的提取

Rule规则解析器:将连接提取器提取到的连接进行请求发送,然后对获取的页面进行指定规则【callback】的解析

一个链接提取器对应唯一一个规则解析器

例:crawlspider深度(全栈)爬取【sunlinecrawl例】

分布式(通常用不到,爬取数据量级巨大、时间少时用分布式)

概念:可将一组程序执行在多态机器上(分布式机群),使其进行数据的分布爬取

原生的scrapy框架是否可以实现分布式?

不能

抽屉

# spider文件

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/1']

  # 连接提取器:从起始url对应的页面中提取符合规则的所有连接;allow=正则表达式
  # 正则为空的话,提取页面中所有连接
  link = LinkExtractor(allow=r'\d+')
  rules = (
    # 规则解析器:将连接提取器提取到的连接对应的页面源码进行指定规则的解析
    # Rule自动发送对应链接的请求
    Rule(link, callback='parse_item', follow=True),
    # 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

阳光热线网

# 1.spider文件
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
from sunLineCrawl.items import SunlinecrawlItem,ContentItem
class SunSpider(CrawlSpider):
  name = 'sun'
  # allowed_domains = ['www.xxx.com']
  start_urls = ['http://wz.sun0769.com/index.php/question/questionType?type=4&page=']

  link = LinkExtractor(allow=r'type=4&page=\d+') # 提取页码连接
  link1 = LinkExtractor(allow=r'question/2019\d+/\d+\.shtml') # 提取详情页连接
  rules = (
    Rule(link, callback='parse_item', follow=False),
    Rule(link1, callback='parse_detail'),
  )
  # 解析出标题和网友名称数据
  def parse_item(self, response):
    tr_list = response.xpath('//*[@id="morelist"]/div/table[2]//tr/td/table//tr')
    for tr in tr_list:
      title = tr.xpath('./td[2]/a[2]/text()').extract_first()
      net_friend = tr.xpath('./td[4]/text()').extract_first()
      item = SunlinecrawlItem()
      item['title'] = title
      item['net_friend'] = net_friend

      yield item

  # 解析出新闻的内容
  def parse_detail(self,response):
    content = response.xpath('/html/body/div[9]/table[2]//tr[1]/td/div[2]//text()').extract()
    content = ''.join(content)
    item = ContentItem()
    item['content'] = content

    yield item
--------------------------------------------------------------------------------
# 2.items文件

import scrapy

class SunlinecrawlItem(scrapy.Item):
  title = scrapy.Field()
  net_friend = scrapy.Field()

class ContentItem(scrapy.Item):
  content = scrapy.Field()
--------------------------------------------------------------------------------
# 3.pipelines文件

class SunlinecrawlPipeline(object):
  def process_item(self, item, spider):
    # 确定接受到的item是什么类型(Content/Sunlinecrawl)
    if item.__class__.__name__ == 'SunlinecrawlItem':
      print(item['title'],item['net_friend'])

    else:
      print(item['content'])

    return item
--------------------------------------------------------------------------------
# 4.setting文件

BOT_NAME = 'sunLineCrawl'

SPIDER_MODULES = ['sunLineCrawl.spiders']
NEWSPIDER_MODULE = 'sunLineCrawl.spiders'

LOG_LEVEL = 'ERROR'

USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36'

ROBOTSTXT_OBEY = False

ITEM_PIPELINES = {
  'sunLineCrawl.pipelines.SunlinecrawlPipeline': 300,
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python爬虫_实现校园网自动重连脚本的教程

python爬虫_实现校园网自动重连脚本的教程

一、背景 最近学校校园网不知道是什么情况,总出现掉线的情况。每次掉线都需要我手动打开web浏览器重新进行账号密码输入,重新进行登录。系统的问题我没办法解决,但是可以写一个简单的pytho...

Python3简单爬虫抓取网页图片代码实例

现在网上有很多python2写的爬虫抓取网页图片的实例,但不适用新手(新手都使用python3环境,不兼容python2), 所以我用Python3的语法写了一个简单抓取网页图片的实例...

使用python爬取微博数据打造一颗“心”

使用python爬取微博数据打造一颗“心”

前言 一年一度的虐狗节终于过去了,朋友圈各种晒,晒自拍,晒娃,晒美食,秀恩爱的。程序员在晒什么,程序员在加班。但是礼物还是少不了的,送什么好?作为程序员,我准备了一份特别的礼物,用以往发...

玩转python爬虫之正则表达式

玩转python爬虫之正则表达式

面对大量杂乱的代码夹杂文字我们怎样把它提取出来整理呢?下面就开始介绍一个十分强大的工具,正则表达式! 1.了解正则表达式 正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特...

Python3爬虫爬取英雄联盟高清桌面壁纸功能示例【基于Scrapy框架】

Python3爬虫爬取英雄联盟高清桌面壁纸功能示例【基于Scrapy框架】

本文实例讲述了Python3爬虫爬取英雄联盟高清桌面壁纸功能。分享给大家供大家参考,具体如下: 使用Scrapy爬虫抓取英雄联盟高清桌面壁纸 源码地址:https://github.co...