python - 为什么我与 Solr 的连接无法工作?

标签 python apache solr scrapy pipeline

我正在使用 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/

相关文章:

python - Sytanx错误: Invalid Sytax

apache - 记录错误日志的URL

Solr-检索找到该单词的文档名称

solr - DSE/Solr : Cannot record QUEUE latency

localization - 韩语分词器

python - Pandas Dataframe 合并其中 1 列匹配,但另一列的值不存在

python - 用单列中的值替换所有列值 - Pandas

python - 根据日期范围合并数据框

apache - 启用 mod_http2 并在 conf 文件中设置协议(protocol)后,HTTP/2 配置未运行

apache - 403 禁止虚拟主机 Ubuntu