0

我有这个规则:

Rule(SgmlLinkExtractor(allow=('http://.*/category/.*/.*/.*',))),
Rule(SgmlLinkExtractor(allow=('http://.*/product/.*', )),cb_kwargs={'crumbs':response.url},callback='parse_item'),

我想将第一个响应传递给函数(parse_item),但问题是这行代码给了我一个错误响应未定义。

如何访问最后一条规则的响应?

4

2 回答 2

3

Response只能在回调中访问该对象,试试这个:

Rule(SgmlLinkExtractor(allow=r'http://.*/category/.*/.*/.*'), callback='parse_cat', follow=True),
Rule(SgmlLinkExtractor(allow=r'http://.*/product/.*'), callback='parse_prod'),

def parse_cat(self, response):
    crumbs = response.url
    return self.parse_item(response, crumbs)

def parse_prod(self, response):
    crumbs = response.url
    return self.parse_item(response, crumbs)

def parse_item(self, response, crumbs):
    ...
于 2013-04-08T16:32:00.520 回答
2

如果你想访问你进入产品的类别url(referer url),在里面parse_item,你可以通过以下方式访问它:

response.request.headers.get('Referer')

通过:#scrapy irc 上的 nyov

于 2014-05-01T06:22:01.760 回答