python網路爬蟲 CrawlSpider使用詳解
阿新 • • 發佈:2020-01-09
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,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,}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。