繁体   English   中英

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

[英]Python Selenium find url and switch to next page

这个 sript 正在工作,但我知道有更多的 urls 可用,我需要添加什么才能切换到下一页。 例如,我喜欢在第一个、第二个和第三个谷歌页面上获取所有网址? 谢谢你的帮助!

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 浏览不同的页面与正常浏览没有什么不同。 创建一个变量并将其设置为一个元素,它将带您进入下一页(下一个按钮或“第 2”页选项)。 然后您可以使用.click()单击该元素。 然后遍历获取您的 url 的部分并为每个连续页面重复。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM