【发布时间】:2020-06-19 11:30:41
【问题描述】:
大家好,这是我的第一个问题。我正在尝试从网站中提取数据。但问题是,它只有在我将鼠标悬停在它上面时才会出现。数据的网站是http://insideairbnb.com/melbourne/。当我将鼠标指针悬停在地图上的点上时,我想从弹出的面板中提取每个列表的入住率。我正在尝试使用此 stackoverflow 帖子 Scrape website with dynamic mouseover event 中的 @frianH 代码。我是使用硒进行数据提取的新手。我有关于 bs4 包的知识。我还没有成功找到正确的 xpath 来完成任务。先感谢您。到目前为止我的代码是
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver import ActionChains
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
browser = webdriver.Chrome(options=chrome_options, executable_path='C:\\Users\\Kunal\\chromedriver.exe')
browser.get('http://insideairbnb.com/melbourne/')
browser.maximize_window()
#wait all circle
elements = WebDriverWait(browser, 20).until(EC.visibility_of_all_elements_located((By.XPATH, '//*[@id="map"]/div[1]/div[2]/div[2]/svg')))
table = browser.find_element_by_class_name('leaflet-zoom-animated')
#move perform -> to table
browser.execute_script("arguments[0].scrollIntoView(true);", table)
data = []
for circle in elements:
#move perform -> to each circle
ActionChains(browser).move_to_element(circle).perform()
# wait change mouseover effect
mouseover = WebDriverWait(browser, 30).until(EC.visibility_of_element_located((By.XPATH, '//*[@id="neighbourhoodBoundaries"]')))
data.append(mouseover.text)
print(data[0])
谢谢你
【问题讨论】:
-
我收到超时异常,因为我的代码无法从网站元素中找到正确的元素
-
浏览器开发工具的“网络”选项卡可以显示包含数据的ajax请求,然后您可以发送请求(可能带有cookie)并获取结构化数据。此外,数据可以在调试器/源选项卡中的脚本中。很有可能你可以在没有硒的情况下完成工作。
标签: python html selenium selenium-webdriver selenium-chromedriver