CrawlSpider
- 作用:用于进行全站数据爬取
- CrawlSpider就是Spider的一个子类
- 如何新建一个基于CrawlSpider的爬虫文件
- scrapy genspider -t crawl xxx /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 scrapyclass 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 = FalseITEM_PIPELINES = { 'sunLineCrawl.pipelines.SunlinecrawlPipeline': 300,}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
- scrapy genspider -t crawl xxx /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 scrapyclass 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 = FalseITEM_PIPELINES = { 'sunLineCrawl.pipelines.SunlinecrawlPipeline': 300,}