簡體   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