如果没有href,如何在Scrapy中链接?

问题描述 投票:0回答:1

当我已经解析了一页并从那里提取信息时,我正在尝试跟踪Scrapy中的链接。问题是,网页没有href,因此我不能随便跟随它。我设法用@ data-param扩展了XPath查询,最后得到了一些东西:page = 2

问题是我不确定如何跟随此链接,因为我想将listName["listLinkMaker"]传递给我的URL生成器或作曲家。

我是否应该再创建一个“ def”,并说它为def parse_pagination来跟踪链接?

代码中使用的JSON非常简单:

[
{"storeName": "Interspar", "storeLinkMaker": "https://popusti.njuskalo.hr/trgovina/Interspar"}
]

下面的代码:

# -*- coding: utf-8 -*-
import scrapy
import json


class LoclocatorTestSpider(scrapy.Spider):
    name = "loclocator_test"
    start_urls = []

    with open("test_one_url.json", encoding="utf-8") as json_file:
        data = json.load(json_file)
        for store in data:
            storeName = store["storeName"]
            storeLinkUrl = store["storeLinkMaker"]
            start_urls.append(storeLinkUrl)

    def parse(self, response):
        selector = "//div[@class='mainContentWrapInner cf']"

        store_name_selector = ".//h1[@class='title']/text()"
        store_branches_selector = ".//li/a[@class='xiti']/@href"

        for basic_info in response.xpath(selector):
            store_branches = {}

            store_branches["storeName"] = basic_info.xpath(store_name_selector).extract_first()
            # This specific XPath extracts 1st part of link needed to crawl all of store branches
            store_branches["storeBranchesLink"] = basic_info.xpath(store_branches_selector).extract_first() + "?"

            store_branches_url = basic_info.xpath(store_branches_selector).extract_first()
            yield response.follow(store_branches_url, self.parse_pagination, meta={"store_branches": store_branches})


    def parse_branches(self, response):
        store_branches_name_selector = "//li[@class='xiti']"
        store_branches = response.meta["store_branches"]

        for store_branch in response.xpath(store_branches_name_selector):
            store_branches["storeBranchName"] = store_branch.xpath(".//span[@class='title']/text()").extract_first()

            yield store_branches

        # This specific XPath extracts 2nd part of link needed to crawl all of store branches
        # URL should look like: https://popusti.njuskalo.hr/trgovina/Interspar?page=n where n>0
        links = response.selector.xpath("//li[@class='next']/button[@class='nBtn link xiti']/@data-param").extract()
        for link in links:
            absolute_url = #LIST FROM FIRST PARSE (ie. store_branches["storeBranchesLink"]) + link
            yield scrapy.Request(absolute_url, callback=self.parse_branches)

谢谢。

python xpath web-scraping scrapy href
1个回答
0
投票

我设法自己找到了一个解决方案,而我与该解决方案比较接近。

下部分:

    # This specific XPath extracts 2nd part of link needed to crawl all of store branches
    # URL should look like: https://popusti.njuskalo.hr/trgovina/Interspar?page=n where n>0
    links = response.selector.xpath("//@data-param").extract()
    store_branches = response.meta["store_branches"]
    for link in links:
        absolute_url = store_branches["storeBranchesLink"]) + link
        yield scrapy.Request(absolute_url, callback=self.parse_branches)

我相信解决方案是添加来自store_branches的响应,因为它能够找到所有可能的页面(?page = n,其中n> 0)。如果由于我对代码的了解还比较初级,所以如果有人知道更多的技术信息,请务必回答。

© www.soinside.com 2019 - 2024. All rights reserved.