Python scrapy爬取小說代碼案例詳解
scrapy是目前python使用的最廣泛的爬蟲框架
架構(gòu)圖如下
解釋:
Scrapy Engine(引擎): 負(fù)責(zé)Spider、ItemPipeline、Downloader、Scheduler中間的通訊,信號、數(shù)據(jù)傳遞等。 Scheduler(調(diào)度器): 它負(fù)責(zé)接受引擎發(fā)送過來的Request請求,并按照一定的方式進(jìn)行整理排列,入隊(duì),當(dāng)引擎需要時,交還給引擎。 Downloader(下載器):負(fù)責(zé)下載Scrapy Engine(引擎)發(fā)送的所有Requests請求,并將其獲取到的Responses交還給Scrapy Engine(引擎),由引擎交給Spider來處理, Spider(爬蟲):它負(fù)責(zé)處理所有Responses,從中分析提取數(shù)據(jù),獲取Item字段需要的數(shù)據(jù),并將需要跟進(jìn)的URL提交給引擎,再次進(jìn)入Scheduler(調(diào)度器), Item Pipeline(管道):它負(fù)責(zé)處理Spider中獲取到的Item,并進(jìn)行進(jìn)行后期處理(詳細(xì)分析、過濾、存儲等)的地方. DownloaderMiddlewares(下載中間件):你可以當(dāng)作是一個可以自定義擴(kuò)展下載功能的組件。Spider Middlewares(Spider中間件):你可以理解為是一個可以自定擴(kuò)展和操作引擎和Spider中間通信的功能組件(比如進(jìn)入Spider的Responses;和從Spider出去的Requests一。安裝
pip install Twisted.whl
pip install Scrapy
Twisted的版本要與安裝的python對應(yīng),https://jingyan.baidu.com/article/1709ad8027be404634c4f0e8.html
二。代碼
本實(shí)例采用xpaths解析頁面數(shù)據(jù)
按住shift-右鍵-在此處打開命令窗口
輸入scrapy startproject qiushibaike 創(chuàng)建項(xiàng)目
輸入scrapy genspiderqiushibaike 創(chuàng)建爬蟲
1>結(jié)構(gòu)
2>qiushibaike.py爬蟲文件
import scrapyfrom scrapy.linkextractors import LinkExtractorfrom scrapy.spiders.crawl import Rule, CrawlSpiderclass BaiduSpider(CrawlSpider): name = ’qiushibaike’ allowed_domains = [’qiushibaike.com’] start_urls = [’https://www.qiushibaike.com/text/’]#啟始頁面# rules= ( Rule(LinkExtractor(restrict_xpaths=r’//a[@class='contentHerf']’),callback=’parse_item’,follow=True), Rule(LinkExtractor(restrict_xpaths=r’//ul[@class='pagination']/li/a’),follow=True) ) def parse_item(self, response): title=response.xpath(’//h1[@class='article-title']/text()’).extract_first().strip() #標(biāo)題 time=response.xpath(’ //span[@class='stats-time']/text()’).extract_first().strip() #發(fā)布時間 content=response.xpath(’//div[@class='content']/text()’).extract_first().replace(’’,’n’) #內(nèi)容 score=response.xpath(’//i[@class='number']/text()’).extract_first().strip() #好笑數(shù) yield({'title':title,'content':content,'time':time,'score':score});
3>pipelines.py 數(shù)據(jù)管道[code]class QiushibaikePipeline:
class QiushibaikePipeline: def open_spider(self,spider):#啟動爬蟲中調(diào)用 self.f=open('xiaoshuo.txt','w',encoding=’utf-8’) def process_item(self, item, spider): info=item.get('title')+'n'+ item.get('time')+' 好笑數(shù)'+item.get('score')+'n'+ item.get('content')+’n’ self.f.write(info+'n') self.f.flush() def close_spider(self,spider):#關(guān)閉爬蟲中調(diào)用 self.f.close()
4>settings.py
開啟ZhonghengPipeline
ITEM_PIPELINES = { ’qiushibaike.pipelines.QiushibaikePipeline’: 300,}
5>0main.py運(yùn)行
from scrapy.cmdline import executeexecute(’scrapy crawl qiushibaike’.split())
6>結(jié)果:
生成xiaohua.txt,里面有下載的笑話文字
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. chat.asp聊天程序的編寫方法2. jsp EL表達(dá)式詳解3. Django程序的優(yōu)化技巧4. XML入門的常見問題(一)5. Python多線程操作之互斥鎖、遞歸鎖、信號量、事件實(shí)例詳解6. IntelliJ IDEA 統(tǒng)一設(shè)置編碼為utf-8編碼的實(shí)現(xiàn)7. Django ORM實(shí)現(xiàn)按天獲取數(shù)據(jù)去重求和例子8. Jsp中request的3個基礎(chǔ)實(shí)踐9. idea設(shè)置自動導(dǎo)入依賴的方法步驟10. 怎樣才能用js生成xmldom對象,并且在firefox中也實(shí)現(xiàn)xml數(shù)據(jù)島?
