python網(wǎng)絡(luò)爬蟲(chóng) CrawlSpider使用詳解
CrawlSpider
- 作用:用于進(jìn)行全站數(shù)據(jù)爬取
- CrawlSpider就是Spider的一個(gè)子類
- 如何新建一個(gè)基于CrawlSpider的爬蟲(chóng)文件
- scrapy genspider -t crawl xxx www.xxx.com
- 例:choutiPro
LinkExtractor連接提取器:根據(jù)指定規(guī)則(正則)進(jìn)行連接的提取
Rule規(guī)則解析器:將連接提取器提取到的連接進(jìn)行請(qǐng)求發(fā)送,然后對(duì)獲取的頁(yè)面進(jìn)行指定規(guī)則【callback】的解析
一個(gè)鏈接提取器對(duì)應(yīng)唯一一個(gè)規(guī)則解析器
例:crawlspider深度(全棧)爬取【sunlinecrawl例】
分布式(通常用不到,爬取數(shù)據(jù)量級(jí)巨大、時(shí)間少時(shí)用分布式)
概念:可將一組程序執(zhí)行在多態(tài)機(jī)器上(分布式機(jī)群),使其進(jìn)行數(shù)據(jù)的分布爬取
原生的scrapy框架是否可以實(shí)現(xiàn)分布式?
不能
抽屜
# 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對(duì)應(yīng)的頁(yè)面中提取符合規(guī)則的所有連接;allow=正則表達(dá)式 # 正則為空的話,提取頁(yè)面中所有連接 link = LinkExtractor(allow=r'\d+') rules = ( # 規(guī)則解析器:將連接提取器提取到的連接對(duì)應(yīng)的頁(yè)面源碼進(jìn)行指定規(guī)則的解析 # Rule自動(dòng)發(fā)送對(duì)應(yīng)鏈接的請(qǐng)求 Rule(link, callback='parse_item', follow=True), # follow:True 將連接提取器 繼續(xù) 作用到 連接提取器提取出來(lái)的連接 對(duì)應(yīng)的頁(yè)面源碼中 ) 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
陽(yáng)光熱線網(wǎng)
# 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+') # 提取頁(yè)碼連接 link1 = LinkExtractor(allow=r'question/2019\d+/\d+\.shtml') # 提取詳情頁(yè)連接 rules = ( Rule(link, callback='parse_item', follow=False), Rule(link1, callback='parse_detail'), ) # 解析出標(biāo)題和網(wǎng)友名稱數(shù)據(jù) 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 # 解析出新聞的內(nèi)容 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, }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Python Scrapy框架:通用爬蟲(chóng)之CrawlSpider用法簡(jiǎn)單示例
- 簡(jiǎn)述python Scrapy框架
- 詳解Python的爬蟲(chóng)框架 Scrapy
- Python爬蟲(chóng)實(shí)例——scrapy框架爬取拉勾網(wǎng)招聘信息
- Python scrapy爬取小說(shuō)代碼案例詳解
- Python Scrapy多頁(yè)數(shù)據(jù)爬取實(shí)現(xiàn)過(guò)程解析
- python3 Scrapy爬蟲(chóng)框架ip代理配置的方法
- python scrapy爬蟲(chóng)代碼及填坑
- Python爬蟲(chóng)Scrapy框架CrawlSpider原理及使用案例
相關(guān)文章
Python:__eq__和__str__函數(shù)的使用示例
這篇文章主要介紹了Python:__eq__和__str__函數(shù)的使用示例,幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下2020-09-09使用Python開(kāi)發(fā)游戲運(yùn)行腳本成功調(diào)用大漠插件
閑來(lái)無(wú)事,想通過(guò)python來(lái)實(shí)現(xiàn)一些簡(jiǎn)單的游戲輔助腳本,而游戲輔助腳本的主要原理就是通過(guò)程序來(lái)查找游戲程序窗口,模擬實(shí)現(xiàn)鼠標(biāo)點(diǎn)擊和鍵盤(pán)按鍵等事件來(lái)實(shí)現(xiàn)游戲輔助的,對(duì)Python開(kāi)發(fā)游戲運(yùn)行腳本相關(guān)知識(shí)感興趣的朋友跟隨小編一起看看吧2021-11-11python模塊與C和C++動(dòng)態(tài)庫(kù)相互調(diào)用實(shí)現(xiàn)過(guò)程示例
這篇文章主要為大家介紹了python模塊與C和C++動(dòng)態(tài)庫(kù)之間相互調(diào)用的實(shí)現(xiàn)過(guò)程示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-11-11python重試裝飾器的簡(jiǎn)單實(shí)現(xiàn)方法
今天小編就為大家分享一篇python重試裝飾器的簡(jiǎn)單實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01Python中WatchDog的使用經(jīng)驗(yàn)總結(jié)
在?python?中文件監(jiān)視主要有兩個(gè)庫(kù),一個(gè)是?pyinotify,一個(gè)是?watchdog,本文主要為大家詳細(xì)介紹一下Python中WatchDog的使用相關(guān)經(jīng)驗(yàn),感興趣的小伙伴可以了解下2023-12-12基于Numba提高python運(yùn)行效率過(guò)程解析
這篇文章主要介紹了基于Numba提高python運(yùn)行效率過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03Python 實(shí)現(xiàn)的 Google 批量翻譯功能
這篇文章主要介紹了Python 實(shí)現(xiàn)的 Google 批量翻譯功能,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-08