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

