這可能是一個非常基本的問題,但我正在練習使用 Selenium 抓取動態頁面的網頁,我想知道是否有一種方法可以只測驗表格部分的網頁抓取,而無需運行整個代碼?我是一個菜鳥,只是沒有看到我做錯了什么嗎?因為我的代碼中有很多延遲,以防止在使用 selenium 單擊按鈕并登錄以進入要抓取表格的頁面時出現錯誤。但是當我一遍又一遍地測驗我的網路抓取以不斷等待整個腳本運行時,這需要很多時間。
uj5u.com熱心網友回復:
添加webdriver.wait到您的腳本并簡化它。
請注意,您必須匯入WebDriverWait和expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver.get("webpage")
wait = WebDriverWait(driver, 20)
enter_username = input('Enter Username: ')
enter_password = input('Enter Password: ')
wait.until(EC.visibility_of_element_located((By.ID,"UserName"))).send_keys(enter_username) #userbox
wait.until(EC.visibility_of_element_located((By.ID,"Password"))).send_keys(enter_password) #password
driver.switch_to.default_content()
wait.until(EC.visibility_of_element_located((By.CLASS_NAME,"btn-primary"))).click() #email box
wait.until(EC.visibility_of_element_located((By.ID,"portlet"))).click() #smart search box
wait.until(EC.visibility_of_element_located((By.ID,"Search"))).send_keys("Search Results") #search box
try:
#Code to click captcha checkbox
#Code to solve recaptcha
except:
print("Recaptcha did not appear")
wait.until(EC.visibility_of_element_located((By.ID,"Submit"))).click() #submit box
#def save_Search_Results():
try:
***#BeautfiulSoup data This is where I'm testing to save data****
print(df)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/444119.html
