小編給大家分享一下Python如何插入Elasticsearch,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
創(chuàng)新互聯(lián)主要從事網(wǎng)頁(yè)設(shè)計(jì)、PC網(wǎng)站建設(shè)(電腦版網(wǎng)站建設(shè))、wap網(wǎng)站建設(shè)(手機(jī)版網(wǎng)站建設(shè))、成都響應(yīng)式網(wǎng)站建設(shè)公司、程序開(kāi)發(fā)、網(wǎng)站優(yōu)化、微網(wǎng)站、成都小程序開(kāi)發(fā)等,憑借多年來(lái)在互聯(lián)網(wǎng)的打拼,我們?cè)诨ヂ?lián)網(wǎng)網(wǎng)站建設(shè)行業(yè)積累了豐富的做網(wǎng)站、成都網(wǎng)站制作、網(wǎng)站設(shè)計(jì)、網(wǎng)絡(luò)營(yíng)銷(xiāo)經(jīng)驗(yàn),集策劃、開(kāi)發(fā)、設(shè)計(jì)、營(yíng)銷(xiāo)、管理等多方位專(zhuān)業(yè)化運(yùn)作于一體。在用scrapy做爬蟲(chóng)的時(shí)候,需要將數(shù)據(jù)存入的es中。網(wǎng)上找了兩種方法,照葫蘆畫(huà)瓢也能出來(lái),暫記下來(lái):
首先安裝了es,版本是5.6.1的較早版本
用pip安裝與es版本相對(duì)的es相關(guān)包
pip install elasticsearch-dsl==5.1.0
方法一:
以下是pipelines.py模塊的完整代碼
# -*- coding: utf-8 -*- # Define your item pipelines here # # Don't forget to add your pipeline to the ITEM_PIPELINES setting # See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html import chardet class SinafinancespiderPipeline(object): def process_item(self, item, spider): return item # 寫(xiě)入到es中,需要在settings中啟用這個(gè)類(lèi) ExchangeratespiderESPipeline # 需要安裝pip install elasticsearch-dsl==5.1.0 注意與es版本需要對(duì)應(yīng) from elasticsearch_dsl import Date,Nested,Boolean,analyzer,Completion,Keyword,Text,Integer,DocType from elasticsearch_dsl.connections import connections connections.create_connection(hosts=['192.168.52.138']) from elasticsearch import Elasticsearch es = Elasticsearch() class AticleType(DocType): page_from = Keyword() # domain報(bào)錯(cuò) domain=Keyword() cra_url=Keyword() spider = Keyword() cra_time = Keyword() page_release_time = Keyword() page_title = Text(analyzer="ik_max_word") page_content = Text(analyzer="ik_max_word") class Meta: index = "scrapy" doc_type = "sinafinance" # 以下settings和mappings都沒(méi)起作用,暫且記下 settings = { "number_of_shards": 3, } mappings = { '_id':{'path':'cra_url'} } class ExchangeratespiderESPipeline(DocType): from elasticsearch6 import Elasticsearch ES = ['192.168.52.138:9200'] es = Elasticsearch(ES,sniff_on_start=True) def process_item(self, item, spider): spider.logger.info("-----enter into insert ES") article = AticleType() article.page_from=item['page_from'] article.domain=item['domain'] article.cra_url =item['cra_url'] article.spider =item['spider'] article.cra_time =item['cra_time'] article.page_release_time =item['page_release_time'] article.page_title =item['page_title'] article.page_content =item['page_content'] article.save() return item
以上方法能將數(shù)據(jù)寫(xiě)入es,但是如果重復(fù)爬取的話(huà),會(huì)重復(fù)插入數(shù)據(jù),因?yàn)?主鍵 ”_id” 是ES自己產(chǎn)生的,找不到自定義_id的入口。于是放棄。
方法二:實(shí)現(xiàn)自定義主鍵寫(xiě)入,覆蓋插入
# -*- coding: utf-8 -*- # Define your item pipelines here # # Don't forget to add your pipeline to the ITEM_PIPELINES setting # See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html from elasticsearch6 import Elasticsearch class SinafinancespiderPipeline(object): def process_item(self, item, spider): return item # 寫(xiě)入到es中,需要在settings中啟用這個(gè)類(lèi) ExchangeratespiderESPipeline # 需要安裝pip install elasticsearch-dsl==5.1.0 注意與es版本需要對(duì)應(yīng) class SinafinancespiderESPipeline(): def __init__(self): self.ES = ['192.168.52.138:9200'] # 創(chuàng)建es客戶(hù)端 self.es = Elasticsearch( self.ES, # 啟動(dòng)前嗅探es集群服務(wù)器 sniff_on_start=True, # es集群服務(wù)器結(jié)點(diǎn)連接異常時(shí)是否刷新es結(jié)點(diǎn)信息 sniff_on_connection_fail=True, # 每60秒刷新節(jié)點(diǎn)信息 sniffer_timeout=60 ) def process_item(self, item, spider): spider.logger.info("-----enter into insert ES") doc = { 'page_from': item['page_from'], 'domain': item['domain'], 'spider': item['spider'], 'page_release_time': item['page_release_time'], 'page_title': item['page_title'], 'page_content': item['page_content'], 'cra_url': item['cra_url'], 'cra_time': item['cra_time'] } self.es.index(index='scrapy', doc_type='sinafinance', body=doc, id=item['cra_url']) return item
搜索數(shù)據(jù)的方法:
# 字典形式設(shè)置body query = { 'query': { 'bool': { 'must': [ {'match': {'_all': 'python web'}} ], 'filter': [ {'term': {'status': 2}} ] } } } ret = es.search(index='articles', doc_type='article', body=query) # 查詢(xún)數(shù)據(jù) data = es.search(index='articles', doc_type='article', body=body) print(data) # 增加 es.index(...) # 修改 es.update(...) # 刪除 es.delete()
完成后
在settings.py模塊中注冊(cè)自定義的類(lèi)
ITEM_PIPELINES = { # 'sinafinancespider.pipelines.SinafinancespiderPipeline': 300, 'sinafinancespider.pipelines.SinafinancespiderESPipeline': 300, }
以上是“Python如何插入Elasticsearch”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計(jì)公司行業(yè)資訊頻道!
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線(xiàn),公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性?xún)r(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿(mǎn)足用戶(hù)豐富、多元化的應(yīng)用場(chǎng)景需求。