首页 > 解决方案 > 使用 selenium 和 python 自动访问下一页

问题描述

我目前正在编写一个 python selenium 脚本来抓取“Likibu.com”,它是一个提供短期住宿的网站,例如 Airbnb,预订......我已经成功获取了第一页中存在的所有数据并保存它们在 csv 文件中,但问题是有 37 页,我也想废弃这些页面中存在的数据。我管理此代码如下:

driver.get("https://www.likibu.com/")
page = driver.page_source
soup = BeautifulSoup(page, "lxml")
driver.get("https://www.likibu.com/{0}".format(soup.find(rel=re.compile("nofollow")).attrs["href"]))

你可以在这里找到网页的源代码:

<ul class="pagination">
<li class="disabled"><a href="#">«</a></li>
<li class="active"><a class="" rel="nofollow" href="https://www.likibu.com/fr/search/39tuzgbpnycdv7tkj102g?guests=2&amp;destination_id=4094&amp;page=1">1</a></li>
<li><a class="" rel="nofollow" href="https://www.likibu.com/fr/search/39tuzgbpnycdv7tkj102g?guests=2&amp;destination_id=4094&amp;page=37">37</a></li>
<li><a class="" rel="nofollow" href="https://www.likibu.com/fr/search/39tuzgbpnycdv7tkj102g?guests=2&amp;destination_id=4094&amp;page=2">»</a></li>

标签: pythonhtmlselenium-webdriverbeautifulsoupselenium-chromedriver

解决方案


我修复了在 True 时使用 boule 的问题:

    if not driver.find_elements_by_xpath("//*[contains(text(), 'Suivant')]"):
        break
    link=WebDriverWait(driver, 1530).until(expected_conditions.element_to_be_clickable((By.LINK_TEXT, "Suivant")))
    link.click()
    next_page = driver.find_element_by_css_selector('#pnnext')
    next_page.click()
    time.sleep(5)"""

推荐阅读