pyspider添加elasticsearch的支持

背景

根据项目的情况,需要将pyspider采集的结果存入es,便于后续的处理。因此需要做以下工作:

  • 在python中安装elasticsearch库
  • 编写基本的es操作类(库)
  • 加入pyspider的库路径中,便于后续引用

 

实施步骤

  • python安装elasticsearch库
pip install elasticsearch

目前默认安装的是7.0.2版本的库,即支持elasticsearch7.x以上的

 

  • 编写基本的es操作类,以下为简单的示例,后续可追加更丰富的功能
    from elasticsearch import Elasticsearch
    
    class EsUtil:
    
        def __init__(self, host='localhost', port=9200):
            self.es = Elasticsearch([{'host': host, 'port': port}])
    
        def info(self):
            return self.es.info()
    
        def get(self, index, id, doc_type='_doc'):
            return self.es.get(index=index, id=id, doc_type=doc_type)
    
        def create_index(self, index, body=None):
            """
            :arg index: The name of the index
            :arg body: The configuration for the index (`settings` and `mappings`)
            """
            # if not self.es.indices.exists(index):
            #    self.es.indices.create(index=index, body=body)
            return self.es.indices.create(index=index, body=body)
    
    

 保存为es_util.py

  • pyspider的库路径

由于我安装的是python3.6,所以不是系统默认的python库路径,经查询,路径为:

/usr/local/python3/lib/python3.6/site-packages/pyspider/libs/

将刚才的es_util.py文件放入该路径即可

  • 在pyspider项目中引用

从pyspider的Handler示例中即可看出,引用规则与库路径一致:from pyspider.libs.xxxx,因此在Handler头部加入:

from pyspider.libs.es_util import EsUtil

并在其中初始化后便可使用了:

    es_util = EsUtil(host='127.0.0.1')

    @every(minutes=24 * 60)
    def on_start(self):
        print(self.es_util.info())
        self.crawl('http://www.xxx.cn/', fetch_type='js', callback=self.index_page)

后续

此后,就可以在on_result里面将采集结果同步到es了

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值