将一个变量分配给xpath scrapy



我使用scratchy来抓取网页,该网页有10多个链接要使用|LinkExtractor抓取,一切都很好,但在抓取提取的链接时,我需要获取页面url。我没有其他方法来获得网址,只能使用

response.request.url

如何将该值分配给

il.add_xpath('url', response.request.url)

如果我这样做,我会得到错误:

          File "C:Python27libsite-packagesscrapyselectorunified.py", line
100, in xpath
            raise ValueError(msg if six.PY3 else msg.encode("unicode_escape"))
        exceptions.ValueError: Invalid XPath: http://www.someurl.com/news/45539/
title-of-the-news

描述如下(仅供参考):

il.add_xpath('descrip', './/div[@class="main_text"]/p/text()')

感谢

加载器有两种向项添加属性的方法,分别是add_xpathadd_value,因此您应该使用以下内容:

...
il.add_value('url', response.url) # yes, response also has the url attribute

最新更新