我正在编写一个解析谷歌搜索结果的CrawlSpider
。搜索查询每次都会更改,因此蜘蛛必须首先连接到数据库以收集有关其需要解析的搜索查询的信息。这是我的anotated CrawlSpider
类:
class GoogleSpider(CrawlSpider):
name = 'googlespider'
allowed_domains = ['google.com', 'google.ca', 'google.fr']
logger = log
_google_query = "http://www.google.{0}/search?q={1}"
def __init__(self, *args, **kwargs):
super(GoogleSpider, self).__init__(*args, **kwargs)
dispatcher.connect(self.get_startup_params, signals.spider_opened)
@defer.inlineCallbacks
def get_startup_params(self, spider, **kw):
# Get the exact requests to issue to google
exreqs = yield get_exactrequests()
# Create the google query (i.e. url to scrape) and store associated information
start_urls = []
self.item_lookup = {}
for keyword, exact_request, lang in exreqs['res']:
url = self.mk_google_query(lang, exact_request)
start_urls.append(url)
self.item_lookup[url] = (keyword, exact_request)
# Assign the google query URLs to `start_urls`
self.start_urls = tuple(start_urls)
def mk_google_query(self, lang, search_terms):
return self._google_query.format(lang, quote(search_terms))
def parse_item(self, response):
sel = Selector(response)
item = Item()
keyword, exact_request = self.item_lookup[response.request.url]
item['urls'] = map(lambda r: r.extract(),
sel.xpath('//h3[@class="r"]/a/@href'))
item['keyword'] = keyword
item['exactrequest'] = exact_request
return item
当我运行scrapy crawl googlespider
时,我得到一个 MASSIVE 日志输出,如下所示:
[ - ]错误:2014-02-17 00:24:38 + 0100 [ - ]错误:2014-02-17 00:24:38 + 0100 [ - ]错误:2014-02-17 00:24:38 + 0100 [ - ]错误: 2014-02-17 00:24:38 + 0100 [ - ]错误:2014-02-17 00:24:38 + 0100 [ - ] 错误:2014-02-17 00:24:38 + 0100 [ - ]错误:2014-02-17 00:24:38 + 0100 [ - ]错误:2014-02-17 00:24:38 + 0100 [ - ]错误:2014-02-17 00:24:38 + 0100 [ - ]错误:2014-02-17 00:24:38 + 0100 [ - ]错误: 2014-02-17 00:24:38 + 0100 [ - ]错误:2014-02-17 00:24:38 + 0100 [ - ] 错误:2014-02-17 00:24:38 + 0100 [ - ]错误:2014-02-17 00:24:38 + 0100 [ - ]错误:2014-02-17 00:24:38 + 0100 [ - ]错误:2014-02-17 00:24:38 + 0100 [ - ]错误:2014-02-17 00:24:38 + 0100 [ - ]错误: 2014-02-17 00:24:38 + 0100 [ - ]错误:2014-02-17 00:24:38 + 0100 [ - ] 错误:2014-02-17 00:24:38 + 0100 [ - ]错误:2014-02-17 00:24:38 + 0100 [ - ]错误:2014-02-17 00:24:38 + 0100 [ - ]错误:2014-02-17 00:24:38 + 0100 [ - ]错误:2014-02-17 00:24:38 + 0100 [ - ]错误: 2014-02-17 00:24:38 + 0100 [ - ]错误:2014-02-17 00:24:38 + 0100 [ - ] 错误:2014-02-17 00:24:38 + 0100 [ - ]错误:2014-02-17 00:24:38 + 0100 [ - ]错误:2014-02-17 00:24:38 + 0100 [ - ]错误:2014-02-17 00:24:38 + 0100 [ - ]错误:2014-02-17 00:24:38 + 0100 [ - ]错误: 2014-02-17 00:24:38 + 0100 [ - ]错误:2014-02-17 00:24:38 + 0100 [ - ] 错误:2014-02-17 00:24:38 + 0100 [ - ]错误:2014-02-17 00:24:38 + 0100 [ - ]错误:2014-02-17 00:24:38 + 0100 [ - ]错误:2014-02-17 00:24:38 + 0100 [ - ]错误:2014-02-17 00:24:38 + 0100 [ - ]错误: 2014-02-17 00:24:38 + 0100 [ - ]错误:2014-02-17 00:24:38 + 0100 [ - ] 错误:2014-02-17 00:24:38 + 0100 [ - ]错误:2014-02-17 00:24:38 + 0100 [ - ]错误:2014-02-17 00:24:38 + 0100 [ - ]错误:2014-02-17 00:24:38 + 0100 [ - ]错误:2014-02-17 00:24:38 + 0100 [ - ]错误: 2014-02-17 00:24:38 + 0100 [ - ]错误:2014-02-17 00:24:38 + 0100 [ - ] 错误:2014-02-17 00:24:38 + 0100 [ - ]错误:2014-02-17 00:24:38 + 0100 [ - ]错误:2014-02-17 00:24:38 + 0100 [ - ]错误:2014-02-17 00:24:38 + 0100 [ - ]错误:2014-02-17 00:24:38 + 0100 [ - ]错误: 2014-02-17 00:24:38 + 0100 [ - ]错误:2014-02-17 00:24:38 + 0100 [ - ] 错误:2014-02-17 00:24:38 + 0100 [ - ]错误:2014-02-17 00:24:38 + 0100 [ - ]错误:2014-02-17 00:24:38 + 0100 [ - ]错误:2014-02-17 00:24:38 + 0100 [ - ]错误:2014-02-17 00:24:38 + 0100 [ - ]错误: 2014-02-17 00:24:38 + 0100 [ - ]错误:2014-02-17 00:24:38 + 0100 [ - ] 错误:2014-02-17 00:24:38 + 0100 [ - ]错误:2014-02-17 00:24:38 + 0100 [ - ]错误:2014-02-17 00:24:38 + 0100 [ - ]错误:2014-02-17 00:24:38 + 0100 [ - ]错误:2014-02-17 00:24:38 + 0100 [ - ]错误: 2014-02-17 00:24:38 + 0100
这个输出继续(我估计)好的10,000行 - 远远超出了我的终端的回滚。
有人知道问题可能是什么以及我应该如何诊断/修复它?
谢谢!
答案 0 :(得分:1)
很难说,因为你的日志几乎没有说什么,但建议采用以下方法:
CrawlerSpider
吗?因为您似乎没有声明任何rule
我认为您应该继承Spider
而不是答案 1 :(得分:1)
事实证明@Rho是正确的:问题源于我呼叫log.start()
的事实。删除对该功能的调用恢复了理智。