单击li按钮的最佳方式是什么?

2024-03-29 06:50:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我在想最好的方法来点击下一页按钮www.booking.com酒店名单和蜘蛛继续运行。你知道吗

检查按钮时:

<li class="nextpage"
   a href="/bigcity/offset=15"class=gotopage_2"
</li>

单页工作代码:

import scrapy
from ..items import BookItem 

class BookSpiderSpider(scrapy.Spider):
    name = "book_spider"
    start_urls = (
        'https://www.booking.com/smallcity/offset=10',
    )

    def parse(self, response) :
        items = BookItem()

        title_name = response.css('span.sr-hotel__name::text').extract()

        items['title_name'] = title_name

        yield items

每次单击按钮时都会更改a href和class

所以我猜python代码应该找到这个按钮,然后使用不同的href将其替换为现有的url并转到那里


Tags: 代码nameimportcomtitlewwwitemsli
2条回答

User.urljoin,为了避免任何URL架构问题:

next_page_url = response.urljoin( next_href )

嗨,请将此代码段用于您的应用程序

next_page = response.xpath('//a[contains(@class,"ficon-caret-right")]/@href').extract()

        if len(next_page) !=0:
            next_href = next_page[0]
            next_page_url = next_href
            print "==============> next cat pagination url :", next_page_url
            yield scrapy.Request(next_page_url, callback=self.parse)

相关问题 更多 >