簡體   English   中英

如何使用 selenium webdriver 提取數據

[英]How to extract data with selenium webdriver

您好,我正在嘗試提取此網頁的賠率: https://www.netbet.fr/derniere-minute?filter=13

這是我的 python 腳本:

#!/usr/bin/python3
# -*- coding: utf­-8 ­-*-

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import os

options = Options()
options.headless = True
options.add_argument("window-size=1400,800")
options.add_argument("--no-sandbox")
options.add_argument("--disable-gpu")
options.add_argument("start-maximized")
options.add_argument("enable-automation")
options.add_argument("--disable-infobars")
options.add_argument("--disable-dev-shm-usage")

driver = webdriver.Chrome(options=options)

driver.get('https://www.netbet.fr/derniere-minute?filter=13')

odds = [my_elem.text for my_elem in WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located((By.XPATH, '//div[contains(@class, "nb-odds_amount")]')))]

print(odds, '\n')

driver.close()
driver.quit()

output 給我的是:

Traceback (most recent call last):
  File "./azerty.py", line 31, in <module>
    odds = [my_elem.text for my_elem in WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located((By.XPATH, '//div[contains(@class, "nb-odds_amount")]')))]
  File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/support/wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 

此腳本與其他網頁完美運行,但在這種情況下並非如此。 一些幫助,謝謝

一些元素被隱藏,這就是問題發生的地方。 你等到所有元素都可見visibility_of_all_elements_located而有些元素被隱藏,所以你將無限等待。 圍繞該問題presence_of_all_elements_located嘗試等待存在而不是對 go 的可見性

odds = [my_elem.text for my_elem in WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.XPATH, '//div[contains(@class, "nb-odds_amount")]')))]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM