python - 使用 matplotlib 和 selenium 绘制实时图

标签 python selenium matplotlib

我目前正在抓取一个网站,并尝试使用 selenium 从网站抓取的信息创建实时图表,并使用 matplotlib 创建图表。我似乎无法让程序创建图表,程序似乎能够从网站获取信息,但图表似乎不起作用,有人可以帮忙吗?下面是代码

我要求程序在打开的窗口中运行必须是一场网球比赛

ff = []

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.common.exceptions import StaleElementReferenceException
from selenium.common.exceptions import NoSuchElementException
import datetime

import matplotlib.pyplot as plt      
import matplotlib.animation as animation
from matplotlib import style


style.use('fivethirtyeight')

   
driver = webdriver.Chrome()  
driver.maximize_window()
wait = WebDriverWait(driver, 50)
 
    
driver.execute_script('window.open("https://livebetting.sportingbet.com/en/live#/8637814","_self")')
                                       
    
python_button = driver.find_elements_by_xpath('//*[@id="scoreboard"]/div[2]/div/lbk-scoreboard-common/div/div[1]/a/span')[0]
python_button.click()
    
statButton = driver.find_elements_by_xpath('//*[@id="scoreboard"]/div[2]/div/lbk-scoreboard-common/div/div[2]/div/div[1]/div/span[1]')[0]
statButton.click()
    
fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)


def animate(i):
    
    title = driver.find_elements_by_xpath('//*[@id="scoreboard"]/div[1]/div[2]/span[3]')[0].text
    import time
    count = 0
    y = []
    x = []
    
    
    
    while title == driver.find_elements_by_xpath('//*[@id="scoreboard"]/div[1]/div[2]/span[3]')[0].text:
        #while driver.find_elements_by_xpath('//*[@id="event"]/lb-marketboard/div/div/div[1]')[0].text != 'We are sorry but no bets are available for this event.'
        try:
           # now = datetime.datetime.now()
           # dat.append(wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="tracker__header"]/div'))).text.splitlines())
            ff.append(wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="scoreboard"]/div[2]/div'))).text.splitlines())
           # ff[len(ff)-1].append(str(now.minute) + ":" + str(now.second))
            ff[len(ff)-1].append(wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="scoreboard"]/div[2]/div/lbk-scoreboard-details/div/div/div[2]/div[1]/div[2]/span'))).get_attribute("class"))
            ff[len(ff)-1].append(wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="scoreboard"]/div[2]/div/lbk-scoreboard-details/div/div/div[2]/div[1]/div[3]/span'))).get_attribute("class"))
            print(ff[len(ff)-1])
            print("\n")
            
            if ff[len(ff)-1][2] != "TB":
                y.append(int(ff[len(ff)-1][17][:ff[len(ff)-1][17].find("%")]))
            else:
                y.append(int(ff[len(ff)-1][18][:ff[len(ff)-1][18].find("%")]))
            
            x.append(count)
            
    
            
        except (TimeoutException,StaleElementReferenceException):
            print('error')
            
        count+=1
        time.sleep(10)
        
        ax1.plot(x,y)
        plt.pause(0.05)
        plt.show()

ani= animation.FuncAnimation(fig, animate, interval=2000)    

我正在尝试根据从网站检索的信息创建实时图表

最佳答案

驱动程序在主线程中运行,并阻止 matplotlib 在窗口中渲染图形。

您可以在单独的线程中运行驱动程序,而让 matplotlib 在主线程中运行。

浏览器线程可以这样实现:

from threading import Thread

class Browser(Thread):
    def __init__(self, plot):
        super().__init__()
        self.plot = plot
        self.mkt_board = None

    def run(self):
        self.driver = webdriver.Chrome()
        self.driver.get("https://livebetting.sportingbet.com/en/live#/8637814")
        homepage = Homepage(self.driver)
        mkt_board = homepage.marketboard()
        mkt_board.transfer_pulse(self.plot)

在这里,我通过在 run 方法中实例化驱动程序来确保驱动程序在线程中运行。

Browser 实例使用声明为的 Plot 实例进行初始化:

class Plot:
    def __init__(self):
        super().__init__()
        style.use('fivethirtyeight')
        x, y = [], []
        fig = plt.figure()
        ax1 = fig.add_subplot(1, 1, 1)
        self.fig = fig
        self.ax1 = ax1
        self.x = x
        self.y = y
        self.ax1.plot(x, y)

    def update(self, x_new, y_new):
        self.x.append(x_new)
        self.y.append(y_new)
        self.ax1.plot(self.x, self.y)
        self.fig.canvas.draw_idle()

    def run(self):
        self.b = Browser(self)
        self.b.start()
        plt.show()

此外,我还使用了Page Objects pattern以便更容易推理流程。

class Page:
    def __init__(self, driver):
        self.driver = driver

    def wait_and_find(self, xpath):
        wait = WebDriverWait(self.driver, 50)
        wait.until(EC.presence_of_element_located((By.XPATH, xpath)))
        return self.driver.find_elements_by_xpath(xpath)[0]

class Homepage(Page):
    def scoreboard(self):
        xpath = '//*[@id="scoreboard"]/div[2]/div/lbk-scoreboard-common/div/div[1]/a/span'
        return self.wait_and_find(xpath)

    def marketboard(self):
        self.scoreboard().click()
        self.stat().click()
        return Marketboard(self.driver)

    def stat(self):
        xpath = '//*[@id="scoreboard"]/div[2]/div/lbk-scoreboard-common/div/div[2]/div/div[1]/div/span[1]'
        return self.wait_and_find(xpath)


class Marketboard(Page):
    NO_BETS = 'We are sorry but no bets are available for this event.'

    def __init__(self, driver):
        self.driver = driver

    def wait_and_find(self, xpath):
        wait = WebDriverWait(self.driver, 50)
        wait.until(EC.presence_of_element_located((By.XPATH, xpath)))
        return self.driver.find_elements_by_xpath(xpath)[0]

    def title(self):
        return self.driver.find_elements_by_xpath('//*[@id="scoreboard"]/div[1]/div[2]/span[3]')[0].text

    def content(self):
        return self.driver.find_elements_by_xpath('//*[@id="event"]/lb-marketboard/div/div/div[1]')[0].text

    def pulse(self, plot):
        '''Replace with info retrieved from your page'''
        while True:
            plot.update(random.randint(0, 100), random.randint(0, 100))
            time.sleep(2)

现在运行绘图。

p = Plot()
p.run()

关于python - 使用 matplotlib 和 selenium 绘制实时图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56182195/

相关文章:

python - Numpy np.any 范围或阈值

python - 使用动态 CSV 命名将 For 循环导出到 CSV

python - Docker Selenium : selenium. common.exceptions.WebDriverException : Message: Service chromedriver unexpectedly exited. 状态代码为:127

java - 使用 Selenium 的多浏览器环境中的 GUI

python - Matplotlib - 在网格中放置饼图

matplotlib - 绘制无限长度的 matplotlib 矩形 block

python - 使用检查点在 Tensorflow Mnist 模型上测试图像

python - Cython 中的 ctypedef 与 numpy : what is right convention?

java - Selenium chromeDriver 崩溃“UnreachableBrowserException/

python - matplotlib 绘图上的主要和次要刻度显示不正确