如何列出使用Scrapy抓取数据的页面的URL?

时间:2019-07-16 15:52:48

标签: scrapy

我是一个真正的初学者,但是我一直在寻找高低的东西,似乎找不到解决方案。我正在构建一些蜘蛛程序,但我不知道如何识别我的抓取数据来自哪个URL。

我的蜘蛛现在非常基础,我正在尝试学习。 我已经尝试过一些在stackoverflow上找到的行,但是除了打印功能之外,其他任何事情都无法正常工作(我不记得它是“ URL:” + response.request.url还是类似的东西。我尝试了一堆东​​西)在代码的解析部分中起作用,但在收益率方面我什么都做不到。

我可以在输出中添加其他标识符,但理想情况下,我想要我正在致力于的项目的URL

import scrapy

class FanaticsSpider(scrapy.Spider):
    name = 'fanatics'

    start_urls = ['https://www.fanaticsoutlet.com/nfl/new-england-patriots/new-england-patriots-majestic-showtime-logo-cool-base-t-shirt-navy/o-9172+t-70152507+p-1483408147+z-8-1114341320',
        'https://www.fanaticsoutlet.com/nfl/new-england-patriots/new-england-patriots-nfl-pro-line-mantra-t-shirt-navy/o-2427+t-69598185+p-57711304142+z-9-2975969489',]

    def parse(self, response):

        yield {
            'sale-price': response.xpath('//span[@data-talos="pdpProductPrice"]/span[@class="sale-price"]/text()').re('[$]\d+\.\d+'),
            #'sale-price': response.xpath('//span[@data-talos="pdpProductPrice"]/span[@class="sale-price"]/text()').get(),
            'regular-price': response.xpath('//span[@data-talos="pdpProductPrice"]/span[@class="regular-price strike-through"]/text()').re('[$]\d+\.\d+'),
            #'regular-price': response.xpath('//span[@data-talos="pdpProductPrice"]/span[@class="regular-price strike-through"]/text()').get(),
              }

非常感谢您的帮助。我尚未开始学习有关管道的任何知识,我不确定这是否可以解决问题?

1 个答案:

答案 0 :(得分:1)

您可以像这样简单地在 yield 中添加网址:

yield {...,
       'url': response.url,
       ...}
相关问题