我正在尝试使用 Apache Solr 抓取一组网页并对其进行索引。为了抓取网页,我在 BeautifulSoup 和 urllib2 的帮助下使用 python。我成功检索了 url 和 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
我该如何纠正这个问题?提前致谢。