我正在使用 Scrapy 抓取网站,然后将该数据发送到 Solr 进行索引。数据通过使用 Solr 的 Python 客户端之一的项目管道发送 - mysolr。
蜘蛛工作正常,我的项目数组有两个具有正确字段的项目。该数组由管道中的 process_item 函数调用。
项目管道
from mysolr import Solr
class SolrPipeline(object):
def __init__(self):
self.client = Solr('http://localhost:8983/solr', version=4)
response = self.client.search(q='Title')
print response
def process_item(self, item, spider):
docs = [
{'title' : item["title"],
'subtitle' : item["subtitle"]
},
{'title': item["title"],
'subtitle': item["subtitle"]
}
]
print docs
self.client.update(docs, 'json', commit=False)
self.client.commit()
这就是我遇到问题的地方。打印的响应是 < SolrResponse status=404 >。我使用了每当启动 Solr 管理 UI 时出现的 SOLR_URL。
我得到的另一个错误如下。
2015-08-25 09:06:53 [urllib3.connectionpool] INFO: Starting new HTTP connection (1): localhost
2015-08-25 09:06:53 [urllib3.connectionpool] DEBUG: Setting read timeout to None
2015-08-25 09:06:53 [urllib3.connectionpool] DEBUG: "POST /update/json HTTP/1.1" 404 1278
2015-08-25 09:06:53 [urllib3.connectionpool] INFO: Starting new HTTP connection (1): localhost
2015-08-25 09:06:53 [urllib3.connectionpool] DEBUG: Setting read timeout to None
2015-08-25 09:06:53 [urllib3.connectionpool] DEBUG: "POST /update HTTP/1.1" 404 1273
这六行出现了两次(我想我尝试添加的每个项目都会出现一次)。
最佳答案
您想要使用 JSON
执行 POST 请求数据,但实际上将 Python 字典列表传递给 self.client.update()
方法。
将 Python 字典列表转换为 JSON:
import json
from mysolr import Solr
class SolrPipeline(object):
def __init__(self):
self.client = Solr('http://localhost:8983/solr', version=4)
response = self.client.search(q='Title')
print response
def process_item(self, item, spider):
docs = [
{'title' : item["title"],
'subtitle' : item["subtitle"]
},
{'title': item["title"],
'subtitle': item["subtitle"]
}
]
docs = json.dumps(docs) # convert to JSON
self.client.update(docs, 'json', commit=False)
self.client.commit()
关于python - 为什么我与 Solr 的连接无法工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32210236/