python网络爬虫CrawlSpider使用详解

  

CrawlSpider
  

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

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

  

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

  

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

  

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

  

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

  

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

  

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

  

不能   

<>强抽屉

        #蜘蛛文件      进口scrapy   从scrapy。linkextractors进口LinkExtractor   从scrapy。蜘蛛进口CrawlSpider,规则      类ChoutiSpider (CrawlSpider):   name=' chouti '   # allowed_domains=(“www.xxx.com”)   start_urls=(“https://dig.chouti.com/1”)      #连接提取器:从起始url对应的页面中提取符合规则的所有连接;允许=正则表达式   #正则为空的话,提取页面中所有连接   链接=LinkExtractor(允许=r \ d +)   规则=(   #规则解析器:将连接提取器提取到的连接对应的页面源码进行指定规则的解析   #规则自动发送对应链接的请求   规则(链接,回调=皃arse_item”,遵循=True),   #:真正的将连接提取器继续作用到连接提取器提取出来的连接对应的页面源码中   )   def parse_item(自我,反应):   项={}   #项目[' domain_id ']=response.xpath('//输入[@ id=" sid "]/@ value ') . get ()   #项目['名称')=response.xpath ('//div [@ id="名称"]). get ()   #项目(“描述”)=response.xpath ('//div [@ id="描述"]). get ()   返回项目      

<>强阳光热线网

        # 1.蜘蛛文件   进口scrapy   从scrapy。linkextractors进口LinkExtractor   从scrapy。蜘蛛进口CrawlSpider,规则   从sunLineCrawl。项目导入SunlinecrawlItem ContentItem   类SunSpider (CrawlSpider):   name=疤簟?   # allowed_domains=(“www.xxx.com”)   start_urls=[' http://wz.sun0769.com/index.php/question/questionType& # 63;类型=4,页面=')      链接=LinkExtractor(允许=r 'type=4,页面=\ d + ') #提取页码连接   link1=LinkExtractor(允许=r 'question/2019 \ d +/\ d + \ .shtml”) #提取详情页连接   规则=(   规则(链接,回调=皃arse_item”,遵循=False),   规则(link1调=' parse_detail '),   )   #解析出标题和网友名称数据   def parse_item(自我,反应):   tr_list=response.xpath ('//* [@ id=" morelist "]/div/表[2]//tr/td/表//tr ')   在tr_list tr:   标题=tr.xpath (’。/td [2]/[2]/text()的).extract_first ()   net_friend=tr.xpath (’。/td [4]/text()的).extract_first ()   项=SunlinecrawlItem ()   项['标题']=标题   项目[' net_friend ']=net_friend      收益项      #解析出新闻的内容   def parse_detail(自我,反应):   内容=response.xpath ('/html表/身体/div [9]/[2]//tr [1]/td/div [2]//text()的).extract ()   内容=" . join(内容)   项=ContentItem ()   项目内容(“内容”)=收益项   --------------------------------------------------------------------------------   # 2.项文件      进口scrapy      类SunlinecrawlItem (scrapy.Item):   title=scrapy.Field ()   net_friend=scrapy.Field ()      类ContentItem (scrapy.Item):   内容=scrapy.Field ()   --------------------------------------------------------------------------------   # 3.管道文件      类SunlinecrawlPipeline(对象):   def process_item(自我,物品,蜘蛛):   #确定接受到的项目是什么类型(内容/Sunlinecrawl)   如果item.__class__。__name__==癝unlinecrawlItem”:   print(项目(“标题”),项目[' net_friend '])      其他:   打印(项目(“内容”))      返回项目   --------------------------------------------------------------------------------   # 4.设置文件      BOT_NAME=' sunLineCrawl '      SPIDER_MODULES=(“sunLineCrawl.spiders”)   NEWSPIDER_MODULE=' sunLineCrawl.spiders '      LOG_LEVEL='错误'      USER_AGENT=' Mozilla/5.0 (Windows NT 10.0;Win64;AppleWebKit x64)/537.36 (KHTML,像壁虎)Chrome/76.0.3809.132 Safari/537.36”      ROBOTSTXT_OBEY=False      ITEM_PIPELINES={   “sunLineCrawl.pipelines。SunlinecrawlPipeline”: 300年,   }

python网络爬虫CrawlSpider使用详解