我的elasticsearch数据库版本为8.4.3
我的python elasticsearch包版本8.4.2
(都是最新版本,浏览器能访问,安装没问题,以下简称es)
我使用scrapy框架爬取数据存储到es中时,报错:
builtins.TypeError: Positional arguments can't be used with Elasticsearch API methods. Instead only use keyword arguments.
settings.py中配置如下:
ELASTICSEARCH_CONNECTION_STRING = 'https://localhost:9200'
ELASTICSEARCH_INDEX = 'movies_scrapy'
代码如下(在settings中,变量connection_string的值是'https://localhost:9200'):
from elasticsearch import Elasticsearch
class ElasticsearchPipeline:
@classmethod
def from_crawler(cls,crawler):
cls.connection_string = crawler.settings.get('ELASTICSEARCH_CONNECTION_STRING')
cls.index = crawler.settings.get('ELASTICSEARCH_INDEX')
return cls()
def open_spider(self,spider):
self.conn = Elasticsearch(self.connection_string)
if not self.conn.indices.exists(self.index):
self.conn.indices.create(index=self.index)
def process_item(self,item,spider):
self.conn.index(index=self.index,body=dict(item),id=hash(item['name']))
return item
def close_spider(self,spider):
self.conn.transport.close()
我将settings.py中ELASTICSEARCH_CONNECTION_STRING变量值改为字典格式后,如下:
ELASTICSEARCH_CONNECTION_STRING = {'host':'localhost','port':9200}
ELASTICSEARCH_INDEX = 'movies_scrapy'
再次运行scrapy后不报上面的错误了,但是报错如下:
TypeError: __init__() missing 1 required positional argument: 'scheme'
对于第二个报错:TypeError: init() missing 1 required positional argument: 'scheme',网上说是版本不兼容导致,但是降低版本太麻烦,有没有不降低版本的解决方法?
Elasticsearch-8.4.3 对应的
"minimum_wire_compatibility_version" : "7.17.0",
"minimum_index_compatibility_version" : "7.0.0"
把 Python 的elasticsearch 包降级到 7.17.0 就行了,插入的代码和连接Elasticsearch的方法都不需要改
python 降包吧 elasticsearch 工程大点 版本不匹配的问题除了降包确实没有其他解决方案
把 python 的es改成
pip install elasticsearch==6.3.1
然后在15行改一下
self.conn.index(index=self.index, doc_type='movie', body=dict(item), id=hash(item['name']))
连接方法就用书上的