Нажав следующую кнопку в Scrapy - PullRequest
0 голосов
/ 25 июня 2019

Я очищаю следующий веб-сайт, https://www.trollandtoad.com/magic-the-gathering/aether-revolt/10066,, и я пытаюсь нажать следующую кнопку, чтобы перейти на следующую страницу и очистить ее.Я сделал это на других программах, и поэтому я просто использую тот же код и только что сделал изменения для работы с текущим сайтом, но он не работает.Он только очищает первую страницу.


    def parse(self, response):
        for game in response.css('div.card > div.row'):
            item = GameItem()
            item["Category"] = game.css("div.col-12.prod-cat a::text").get()
            item["Card_Name"]  = game.css("a.card-text::text").get()
            for buying_option in game.css('div.buying-options-table div.row:not(:first-child)'):
                item["Seller"] = buying_option.css('div.row.align-center.py-2.m-auto > div.col-3.text-center.p-1 > img::attr(title)').get()
                item["Condition"] = buying_option.css("div.col-3.text-center.p-1::text").get()
                item["Price"] = buying_option.css("div.col-2.text-center.p-1::text").get()
                yield item
            next_page = response.xpath('//a[contains(., "Next Page")]/@href').get()
            # If it exists and there is a next page enter if statement
            if next_page is not None:
                # Go to next page
                yield response.follow(next_page, self.parse)

ОБНОВЛЕНИЕ # 1

Вот снимок HTML-кода для следующей кнопки

HTML for next page

ОБНОВЛЕНИЕ № 2

Вот обновленный код, который я должен попробовать и перейти на следующую страницу.Все еще не работает, но я думаю, что я ближе к правильному коду.

next_page = response.xpath('//div[contains(., "Next Page")]/@class').get()
            # If it exists and there is a next page enter if statement
            if next_page is not None:
                # Go to next page
                yield response.follow(next_page, self.parse)

1 Ответ

1 голос
/ 27 июня 2019

Вам необходимо найти номер следующей страницы и после этого отправить форму, используя ее:

def parse(self, response):

    for game in response.css('div.card > div.row'):
        item = GameItem()
        item["Category"] = game.css("div.col-12.prod-cat a::text").get()
        item["Card_Name"]  = game.css("a.card-text::text").get()
        for buying_option in game.css('div.buying-options-table div.row:not(:first-child)'):
            item["Seller"] = buying_option.css('div.row.align-center.py-2.m-auto > div.col-3.text-center.p-1 > img::attr(title)').get()
            item["Condition"] = buying_option.css("div.col-3.text-center.p-1::text").get()
            item["Price"] = buying_option.css("div.col-2.text-center.p-1::text").get()
            yield item
    next_page_number = response.xpath('//div[div[.="Next Page"]][not(contains(@class, "hide"))]/@data-page').get()
    # If it exists and there is a next page enter if statement
    if next_page_number:
        yield scrapy.FormRequest.from_response(
            response=response,
            formid="category_form",
            formdata={
                'page-no': next_page_number,
            },
            callback=self.parse
        )
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...