【问题标题】:Automat acess to next page using selenium and python使用 selenium 和 python 自动访问下一页
【发布时间】:2018-11-03 10:26:10
【问题描述】:

我目前正在编写一个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>

【问题讨论】:

    标签: python html selenium-webdriver beautifulsoup selenium-chromedriver


    【解决方案1】:

    任何时候你要废弃多个页面,你必须弄清楚 url 是如何变化的。在你的情况下:

    root = 'https://www.likibu.com/fr/search/39yrzgbpnycdv7tkj132g?guests=2&page='
    
    page_number = 0
    while true:
        page_number +=1
        try: 
            url = root + str(page_number)
            ### CODE #####
        except:
            ### terminare / print something ####
    

    注意:我在您发布的链接中添加了“&page=”。尽管如此,它不会显示在第一页的 url 中。它仍然退出。你添加'&page=1',它会给玩具第一页。

    【讨论】:

      【解决方案2】:

      我在使用 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)"""
      

      【讨论】:

        猜你喜欢
        • 2021-05-08
        • 1970-01-01
        • 1970-01-01
        • 2022-01-12
        • 1970-01-01
        • 1970-01-01
        • 2020-07-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多