0

我正在使用 solr 和 python。我使用 urllib 库来获取 http 数据。

我将代码编写为:

from urllib2 import * 
connection = urlopen('http://localhost:8983/solr/data/select?indent=on&q=sender_name:*AXI*&wt=json')

它工作正常,但是当我应用更多查询过滤器字符串时,如下所示:

from urllib2 import * 
connection = urlopen('http://localhost:8983/solr/data/select?indent=on&q=sender_name:*AXI* AND message:*Avbl*&wt=json')

我收到错误消息:

HTTPError                                 Traceback (most recent call last)
<ipython-input-22-6dad7f9847f1> in <module>()
----> 1 connection = urlopen('http://localhost:8983/solr/data/select?indent=on&q=sender_name:*AXI* AND message:*Avbl*&wt=json')

/usr/lib/python2.7/urllib2.pyc in urlopen(url, data, timeout, cafile, capath, cadefault, context)
    152     else:
    153         opener = _opener
--> 154     return opener.open(url, data, timeout)
    155 
    156 def install_opener(opener):

/usr/lib/python2.7/urllib2.pyc in open(self, fullurl, data, timeout)
    433         for processor in self.process_response.get(protocol, []):
    434             meth = getattr(processor, meth_name)
--> 435             response = meth(req, response)
    436 
    437         return response

/usr/lib/python2.7/urllib2.pyc in http_response(self, request, response)
    546         if not (200 <= code < 300):
    547             response = self.parent.error(
--> 548                 'http', request, response, code, msg, hdrs)
    549 
    550         return response

/usr/lib/python2.7/urllib2.pyc in error(self, proto, *args)
    471         if http_err:
    472             args = (dict, 'default', 'http_error_default') + orig_args
--> 473             return self._call_chain(*args)
    474 
    475 # XXX probably also want an abstract factory that knows when it makes

/usr/lib/python2.7/urllib2.pyc in _call_chain(self, chain, kind, meth_name, *args)
    405             func = getattr(handler, meth_name)
    406 
--> 407             result = func(*args)
    408             if result is not None:
    409                 return result

/usr/lib/python2.7/urllib2.pyc in http_error_default(self, req, fp, code, msg, hdrs)
    554 class HTTPDefaultErrorHandler(BaseHandler):
    555     def http_error_default(self, req, fp, code, msg, hdrs):
--> 556         raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
    557 
    558 class HTTPRedirectHandler(BaseHandler):

HTTPError: HTTP Error 400: Unknown Version

如何解决此错误。

4

1 回答 1

1

URL 不包含空格字符。当它是查询时,您可以用“%20”替换空格字符。

于 2018-06-13T13:55:38.683 回答