【问题标题】:(xPathHelp) Scrapy not going to next page only scrapes first page(xPathHelp) Scrapy 不去下一页只抓取第一页
【发布时间】:2023-03-30 17:25:01
【问题描述】:

我试图在 href Here 中提取嵌套的 url,这样我就可以为我的蜘蛛创建一个“下一页”xpath 选择器,但我无法找出正确的位置路径。
我一直在 Scrapy shell 环境中测试我的代码

这是我的蜘蛛源代码 - 使用 python3

import scrapy class StarbucksSpider(scrapy.Spider):
name = 'starbucks'
allowed_domains = ['gameflip.com/shop/gift-cards/starbucks']
start_urls = ['https://gameflip.com/shop/gift-cards/starbucks?limit=36&platform=starbucks&accept_currency=USD&status=onsale']

def parse(self, response):
    
    slots = response.xpath('//*[@class="listing-detail view-grid col-6 col-md-4 col-lg-3 col-xl-2"]')

    for slot in slots:

        fullPrice = slot.xpath('.//*[@class="col-12 description normal"]/text()').extract_first()
        Discount = slot.xpath('.//*[@class="badge badge-success listing-discount"]/text()').extract_first()
        price = slot.xpath('.//*[@class="money"]/text()').extract_first()
        status = slot.xpath('.//*[@alt="sold"]/@alt').extract_first()

        print ('\n')
        print (status)
        print (fullPrice)
        print (Discount)
        print (price)
        print ('\n')

        next_PageUrl = response.xpath('//*[@class="btn"]/@href').extract_first()
        absoulute_next_page_url = response.urljoin(next_PageUrl)
        yield scrapy.Request(absoulute_next_page_url)

请不要犹豫,向我提问以更好地帮助您回答。 任何帮助表示赞赏;D

感谢您的宝贵时间和回答!

【问题讨论】:

  • 我认为你需要 selenium 或 scrapy-splash 来提取数据。这个网站使用了不抓取scrapy的javascript。
  • 网站 url 正在使用 API 来填充页面。你可以看到输出here。您可以使用requesturllib 库来处理获取。否则@SamsulIslam 上面所说的是你如何处理 javascript DOM。

标签: python-3.x web-scraping xpath scrapy scrapy-shell


【解决方案1】:

正如jwjhdev 的评论中所述,内容来自 API。 重新加载页面时,您可以在浏览器开发工具的网络选项卡中看到这一点。 可以修改 API 的 url,以便为每页提供更多或更少的对象。 如果我们在你的情况下增加到150,我们只会得到一页数据,这意味着一个请求:https://production-gameflip.fingershock.com/api/v1/listing?limit=150&kind=item&category=GIFTCARD&platform=starbucks&status=onsale&sort=_score:desc,shipping_within_days:asc,created:desc&accept_currency=USD

因此,我们可以查询 api 并获取更易于操作的结构化数据,而不是从网页获取数据并不得不使用 xpaths。

我在下面稍微修改了您的蜘蛛代码,以展示我们如何从 API 获取数据。我们将使用上面的 API url 作为start_urls 之一 但是我注意到折扣不在响应中,所以我相信您必须在代码中计算它。

import json
import scrapy


class StarbucksSpider(scrapy.Spider):
    name = 'starbucks'
    start_urls = [
        'https://production-gameflip.fingershock.com/api/v1/listing?limit=150&kind=item&category=GIFTCARD&platform=starbucks&status=onsale&sort=_score:desc,shipping_within_days:asc,created:desc&accept_currency=USD'
    ]


    def parse(self, response):
        api_data = json.loads(response.text)
        slots = api_data.get('data')

        for slot in slots:
            fullPrice = slot.get('name')
            # I couldn't find in the json the discount
            Discount = 'TODO - Calculate discount using values from API'
            price = slot.get('price')
            status = slot.get('status')

            print('\n')
            print(status)
            print(fullPrice)
            print(Discount)
            print(price)
            print('\n')

【讨论】:

  • 使用这种方法,提取多个页面的最常见做法是什么,而不仅仅是start_urls。我必须放置多个 API URL 吗?非常感谢您的所有帮助!
  • 你应该使用 url 的 api 来尝试看看它是如何工作的。如果您删除平台类型(在上面的例子中,平台是星巴克),您会从网站的所有平台获得杯子:production-gameflip.fingershock.com/api/v1/…
  • 如果您只想报废,可以说:亚马逊、沃尔玛和星巴克:使用docs.scrapy.org/en/latest/topics/… 方法并向每个平台发出请求:'https://production-gameflip.fingershock.com/api/v1/listing?limit=150&kind=item&category=GIFTCARD&platform={platform}&status=onsale&sort=_score:desc,shipping_within_days:asc,created:desc&accept_currency=USD'.format(platform=my_platform_from_loop)
猜你喜欢
  • 1970-01-01
  • 2023-01-24
  • 1970-01-01
  • 2015-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-30
相关资源
最近更新 更多