Python Selenium 查找 url 并切换到下一页

标签 python selenium

此脚本正在运行,但据我所知,还有更多可用的网址,我需要添加什么才能切换到下一页。 例如,我想获取第一个、第二个和第三个谷歌页面上的所有网址? 感谢您的帮助!

import time
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from bs4 import BeautifulSoup

options = Options()
options.headless = True
driver = webdriver.Firefox(options=options, executable_path = "c:\\...\\geckodriver.exe")
url = "https://www.google.de"
driver.get(url)

actions = webdriver.ActionChains(driver)

time.sleep(4)

element = driver.find_element_by_xpath("//form[@id='tsf']/div[2]/div/div/div/div[2]/input")
element.send_keys('restaurant in hamburg');
element.submit()
time.sleep(2)

driver = webdriver.Firefox(browser_profile=fp)

htmltext = driver.page_source
soup = BeautifulSoup(htmltext, "lxml")
ergebnisse = soup.find_all ("div", class_="rc")

for result in results:
    link = result.find ("a",href=True)
    print (link.get('href'))

driver.quit()

最佳答案

通过 selenium 导航不同的页面与正常浏览没有任何不同。创建一个变量并将其设置为一个元素,该元素将带您进入下一页(下一页按钮或“第二页”选项)。然后,您可以使用 .click() 单击该元素。然后循环遍历获取网址的部分,并对每个连续页面重复。

关于Python Selenium 查找 url 并切换到下一页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59804363/

相关文章:

python - 尽管使用了allow_growth=True,为什么keras model.fit 使用了这么多内存?

python - 如何构建本地化数据库?

Python语法。美丽,正确,简短的代码

python - WebDriver python 相当于 "refreshed"预期条件

python - 写入 JSON - 将\u00a3 转换为 £

python - scipy distance_transform_edt 函数如何工作?

python -\frac{}{} 不会为我工作 w/pylab

python - 元素在模态窗口中不可见 - Selenium Python

python - 我有一个日历选择器。如何使用 Selenium 和 Python 选择可用日期?

Selenium 网络驱动程序 : What is the substitute for browserbot?