python - 使用 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&destination_id=4094&page=1">1</a></li>
<li><a class="" rel="nofollow" href="https://www.likibu.com/fr/search/39tuzgbpnycdv7tkj102g?guests=2&destination_id=4094&page=37">37</a></li>
<li><a class="" rel="nofollow" href="https://www.likibu.com/fr/search/39tuzgbpnycdv7tkj102g?guests=2&destination_id=4094&page=2">»</a></li>
解决方案
我修复了在 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)"""
推荐阅读
- google-sheets - 将“特定”文本添加到列时需要填充字段
- html - 如何在每 3 个项目之后将 css 应用于 3 个项目?
- mysql - MySQL 和预定义标签:一张表解决方案?
- javascript - 图像未覆盖画布的整个区域
- python-3.x - 启用检查按钮时无法执行 if else 语句代码
- django - CreateModelMixin TypeError: __init__() 接受 1 个位置参数,但给出了 2 个
- html - HTML 动画光标
- android - How to generate CSV/XLS/XLSX from an ArrayList values using Android Studio?
- ios - 在上传到应用商店之前,可以使用真实的 Admob id 在模拟器上测试广告吗
- sql-server - 获取当前日期然后将每月输出 2 个截止日期