【问题标题】:Scraping dynamic dropdown with Selenium in Python在 Python 中使用 Selenium 抓取动态下拉列表
【发布时间】:2021-06-17 10:37:23
【问题描述】:

我希望对以下内容有所帮助。我正在尝试在此网站上抓取代码下拉列表的元素:https://live.hxro.io/tixwix

我的代码如下使用 selenium

from selenium import webdriver
from selenium.webdriver.support.select import Select
from selenium.webdriver.chrome.options import Options

url = "https://live.hxro.io/tixwix"
chrome_options = Options()
chrome_options.add_argument("--headless")
driver = webdriver.Chrome(executable_path = r'C:\geckodriver\chromedriver.exe',options = chrome_options)
driver.get(url)

tickers = driver.find_elements_by_class_name('lastprice-toggle')

tickers[0].text

这只会返回

'BTC\nLast Price\n$39,255.07'

由于它是一个 Ajax 调用,我不确定如何以有效的方式检索其他代码。我以为函数 find_element's' 会将所有元素返回到一个列表中,但我只得到第一个 tickers[1] 超出范围。

页面源截图: enter image description here

谢谢

【问题讨论】:

    标签: python ajax selenium web-scraping


    【解决方案1】:

    以下代码在我的本地运行良好:

    解释

    您需要click on accept cookies 按钮并尝试使用ExplicitWait 加上您需要单击下拉菜单中的 svg 图标(XPATH//span[text()='Last Price']/../following-sibling::*)。

    最后span.moon 包含您要查找的所有元素。

    代码:

    executablePath = r'C:\geckodriver.exe'
    options = webdriver.FirefoxOptions()
    options.add_argument("--headless")
    driver = webdriver.Firefox(executable_path = executablePath, options=options)
    driver.maximize_window()
    driver.get("https://live.hxro.io/tixwix")
    wait = WebDriverWait(driver, 10)
    wait.until(EC.element_to_be_clickable((By.ID, "onetrust-accept-btn-handler"))).click()
    wait.until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Last Price']/../following-sibling::*"))).click()
    sleep(5)
    for values in driver.find_elements(By.CSS_SELECTOR, "span.moon"):
        print(values.get_attribute('innerHTML'))
    

    输出:

    $39,321.02
    $39,321.02
    $8,728.5
    $0.31063
    $2,435.337
    $24.232
    $40.99
    $8.9730
    $22.685
    $37,234.01
     HIGHER
    <span class="tooltip-text">CLOSE ABOVE</span>
    (CLOSE ABOVE)
    (TOUCH)
     $100,000
    14.20X
     $100,000
    1000.05X
     $68,000
    152.69X
     $66,000
    127.38X
     $64,000
    117.34X
     $63,000
    112.25X
     $62,000
    105.95X
     $61,000
    4.490X
     $60,000
    92.80X
     $44,000
    4.790X
    
    Process finished with exit code 0
    

    【讨论】:

    • 感谢您的回复。我收到您的代码错误。 ElementClickInterceptedException:消息:元素点击被拦截:元素 在点 (490 , 204)。
    • 这很奇怪..它在我的机器上工作得很好
    • 你有driver.maximize_window() 吗?
    • 我得到了上面的输出,应该对你有用。
    • 谢谢,这两个答案实际上都非常有帮助。
    【解决方案2】:

    这里有几个问题:

    1. 你应该添加
    options.add_argument("--start-maximized")
    
    1. 您应该添加等待/延迟
    2. 您应该打开下拉列表
    3. 使用正确的定位器获取下拉列表中的元素
      UPD
      5)如果是ElementClickInterceptedException,请用JavaScript点击它。不是最佳做法,但会起作用。
      我认为代码应该是这样的:
    from selenium import webdriver
    from selenium.webdriver.support.select import Select
    from selenium.webdriver.chrome.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
    
    wait = WebDriverWait(driver, 30)
    
    url = "https://live.hxro.io/tixwix"
    chrome_options = Options()
    chrome_options.add_argument("--headless")
    options.add_argument("--start-maximized")
    driver = webdriver.Chrome(executable_path = r'C:\geckodriver\chromedriver.exe',options = chrome_options)
    driver.get(url)
    
    last_price_toggle = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, '.lastprice-toggle')))
    driver.execute_script("arguments[0].click();", last_price_toggle)
    
    wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, '.lastprice-item')))
    
    tickers = driver.find_elements_by_css_selector('div.lastprice-item span.moon')
    for ticker in tickers:
        print(ticker.text)
    

    【讨论】:

    • 像 Cruisepandey 的代码一样,我得到一个错误。 ElementClickInterceptedException:消息:元素点击被拦截:元素
      ...
      在点 (399, 203) 不可点击。其他元素会收到点击:
      ...
      (会话信息:headless chrome=91.0.4472.106)
    • 嗨,Prophet,如上所述,我已切换到 firefox,代码现在可以正常工作。很抱歉给您带来麻烦。谢谢
    猜你喜欢
    • 2021-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-01
    • 1970-01-01
    • 2021-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多