我正在尝试抓取一组网页并使用Apache Solr对其进行索引。要抓取网页,我在BeautifulSoup和urllib2的帮助下使用python。我成功检索了网址和HTML数据。
现在我正在尝试让Solr通过solr(http://code.google.com/p/solrpy/)为它们编制索引。我一直收到Http 404错误。
我没有修改默认的schema.xml,我正在使用Apache Solr附带的示例服务器。
这是我的代码:
import sys
import urllib2
import solr
from bs4 import BeautifulSoup
from lxml import etree
import hashlib
solrUrl = 'http://localhost:8983/solr/'
solrInstance = solr.SolrConnection(solrUrl)
conn = urllib2.urlopen('http://seekingalpha.com/market_currents.xml')
root = etree.fromstring(conn.read())
links = root.findall(".//link")
counter = 0
for link in links:
counter=counter+1
url = link.text
url_md5 = hashlib.md5(url).hexdigest()
conn = urllib2.urlopen(link.text)
soup = BeautifulSoup(conn.read())
title_page = soup.html.head.title.string.decode("utf-8")
print title_page
try: # Add to the Solr instance
solrInstance.add(id=str(url_md5),url_s=url,text=str(title_page),title=str(title_page))
except Exception as inst:
print "Error adding URL: "+url
print "\tWith Message: "+str(inst)
else:
print "Added Page \""+title+"\" with URL "+url
try:
solrInstance.commit()
except:
print "Could not Commit Changes to Solr Instance - check logs"
else:
print "Success. "+str(counter)+" documents added to index"
这是错误:
Error adding URL: http://seekingalpha.com/currents/all
With Message: HTTP code=404, reason=Not Found
我该如何纠正这个问题?提前谢谢。
答案 0 :(得分:2)
我自己没有使用solrpy
,但在玩完它之后,您似乎必须删除solr网址中的尾随/
。将其更改为
solrUrl = 'http://localhost:8983/solr'