0
因此,我有一个抓取页面的蜘蛛,为它遇到的每个项目收集数据。如果该项目没有选项,则只需将该项目沿管道发送。如果有选项,它将汇编一个选项列表列表,并为每个唯一的选项组合发送一个请求(作为HTML代码片段返回,所以我把它当作XML处理)。对于每个选项组合,它提取项目的价格并将其发送到管道。只有它没有。Scrapy:为抓取页面中的选项选择创建多个项目
下面是一些代码:
#spider code above here that does all the normal stuff,
#plus gets and organize all options. Then this:
for optLists in uberList:
queryString = '?func=Options¤tOption=1&Modal=False&AddUniqueID=False&sku=' + sku + '&option1=' + optLists[0] + '&option2=' + optLists[1] + '&option3=' + optLists[2]
reqURL = urljoin(baseAjaxURL, queryString)
req = Request(url=reqURL,
callback=self.parse_ajax,
meta = {'item' : item},
)
self.log('simplified item: ' + reqURL, level=log.DEBUG)
yield req
而且回调函数:
def parse_ajax(self, response):
print 'parsing ajax'
xxs = XmlXPathSelector(response)
item = response.meta['item']
item['price'] = xxs.select("normalize-space(substring-before(substring-after(.//skuMainPrice/text(), 'ppPrice:'),'/span'))").extract()[0]
print 'parse_ajax price: ', item['price']
return item
的在第一种方法火灾循环正常,一旦每组选项。如果回调是不存在的方法(这很好),那么Request会抛出一个错误,但回调方法中的print语句不会触发,也不会将该项传播到管道中。
任何意见我做错了什么或如何做到这一点正确的将不胜感激。
感谢
通常当请求失败时会发生此行为,因此不会调用回调函数。你可以验证AJAX网址的请求是否正确? –
我已经打印出reqURL并在Firefox中跟随链接。这给了我预期的HTML片段。 – GMBill
你也可以验证Scrapy处理200 OK的请求吗? –