我想在以下方面得到一些帮助。我正试图在这个网站上抓取股票行情下拉列表的元素: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:geckodriverchromedriver.exe',options = chrome_options)
driver.get(url)
tickers = driver.find_elements_by_class_name('lastprice-toggle')
tickers[0].text
这只会返回
'BTCnLast Pricen$39,255.07'
由于这是一个Ajax调用,我不知道如何以有效的方式检索其他ticker。我以为函数find_element的'会将所有元素返回到列表中,但我只得到第一个tickers[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
这里有几个问题:
- 您应该添加
options.add_argument("--start-maximized")
- 您应该添加一个等待/延迟
- 你应该打开下拉列表
- 使用正确的定位器获取下拉列表中的元素
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:geckodriverchromedriver.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)