你好,我在 python 中使用 selenium webdriver for chrome 并試圖從中抓取一些資料 https://www.google.com/travel/things-to-do
我在這里專注于可以在這里看到的地方描述:

因此,為了獲得每個單獨的描述,我必須按下吸引力并將 html 保存到串列中,以便將來使用 BeautifulSoup 進行決議。
每次點擊都會重繪 頁面,所以我正在考慮以某種方式計算所有顯示的景點,然后在回圈中單擊每個景點并保存描述。
任何人都知道如何處理它?繼承人簡單的代碼,讓你到我被卡住的地方
chrome_options = webdriver.ChromeOptions()
#chrome_options.headless = True
chrome_options.add_argument('--incognito')
#chrome_options.add_argument('--headless')
s=Service(ChromeDriverManager().install())
driver = webdriver.Chrome(executable_path=r"\\chromedriver.exe", options=chrome_options, service=s)
driver.get("https://www.google.com/travel/things-to-do/see-all?dest_mid=/m/081m_&dest_state_type=sattd&dest_src=yts&q=Warszawa#ttdm=52.227486_21.004941_13&ttdmf=%2Fm%2F0862m")
# If you are not running webdriver in incognito mode you might skip the below button since it goes through accepting cookies
button = driver.find_element_by_xpath("/html/body/c-wiz/div/div/div/div[2]/div[1]/div[4]/form/div[1]/div/button")
button.click()
time.sleep(1)
objects = driver.find_elements_by_class_name('f4hh3d')
for k in objects:
k.click()
time.sleep(5)
uj5u.com熱心網友回復:
對于每個景點索引,您可以單擊它打開詳細資訊,獲取詳細資訊,關閉詳細資訊,再次獲取景點串列并前往下一個景點。
這樣的事情應該作業:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver.get("https://www.google.com/travel/things-to-do/see-all?dest_mid=/m/081m_&dest_state_type=sattd&dest_src=yts&q=Warszawa#ttdm=52.227486_21.004941_13&ttdmf=%2Fm%2F0862m")
wait = WebDriverWait(driver, 20)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.f4hh3d")))
time.sleep(1)
attractions = driver.find_elements_by_css_selector('div.f4hh3d')
for i in range(len(attractions)):
attractions = driver.find_elements_by_css_selector('div.f4hh3d')
attractions[i].click()
description = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'div[jsname="BEZjkb"] div[jsname="bN97Pc"]'))).text
#do with the description what you want
#close the attraction by clicking the button
driver.find_element_by_css_selector('div.reh1ld button')
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/394122.html
下一篇:獲得串列第一個單值的最佳方法
