gpt4 book ai didi

python - 使用动态鼠标悬停事件抓取网站

转载 作者:行者123 更新时间:2023-12-05 00:47:11 27 4
gpt4 key购买 nike

我正在尝试抓取从鼠标悬停事件动态生成的数据。我想从
的哈希率分布图中获取信息 https://slushpool.com/stats/?c=btc这是在您滚动每个圆圈时生成的。

下面的代码从网站获取html数据,并返回一旦鼠标经过一个圆圈就被填充的表格。但是,我无法弄清楚如何触发每个圆圈的鼠标悬停事件以填充表格。

from lxml import etree
from xml.etree import ElementTree
from selenium import webdriver

driver_path = "#Firefox web driver"
browser = webdriver.Firefox(executable_path=driver_path)
browser.get("https://slushpool.com/stats/?c=btc")


page = browser.page_source #Get page html
tree = etree.HTML(page) #create etree

table_Xpath = '/html/body/div[1]/div/div/div/div/div[5]/div[1]/div/div/div[2]/div[2]/div[2]/div/table'

table =tree.xpath(table_Xpath) #get table using Xpath

print(ElementTree.tostring(table[0])) #Returns empty table.
#Should return data from each mouseover event

有没有办法为每个圆圈触发 mouseover 事件,然后提取生成的数据。

提前感谢您的帮助!

最佳答案

要触发每个圆圈的鼠标悬停事件,您必须为 visibility_of_all_elements_located() 诱导 WebDriverWait,您可以使用以下 Locator Strategies :

  • 代码块:

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.action_chains import ActionChains

    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument("start-maximized")
    chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
    chrome_options.add_experimental_option('useAutomationExtension', False)
    driver = webdriver.Chrome(options=chrome_options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
    driver.get("https://slushpool.com/stats/?c=btc")
    driver.execute_script("return arguments[0].scrollIntoView(true);", WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//h1//span[text()='Distribution']"))))
    elements = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//h1//span[text()='Distribution']//following::div[1]/*[name()='svg']//*[name()='g']//*[name()='g' and @class='paper']//*[name()='circle']")))
    for element in elements:
    ActionChains(driver).move_to_element(element).perform()
  • 浏览器快照:

Action

关于python - 使用动态鼠标悬停事件抓取网站,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57901045/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com