使用Selenium和Python抓取数据

时间:2020-01-15 20:25:26

标签: python css selenium selenium-webdriver web-scraping

我仅在游戏进行时需要获取此数据,但我无法获取蓝色值: Image

这些是我无法获得的元素,因为它们都具有相同的名称,包括未进行游戏的名称:

<span class="bet-button-price">3.1</span> <span class="bet-button-size">3.1</span>

我设法弄清楚时间,结果是我的代码:

from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from time import sleep



x = webdriver.Chrome(executable_path='G:\Python\Driver\chromedriver')

x.get('https://www.betfair.it/exchange/plus/football/inplay')

#Wait
wait = WebDriverWait(x, 100)
#Read Minute
wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'middle-label')))
#Read Result
wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'home')))
wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'away')))
#Read Bet
wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'bet-button-price')))
wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'bet-button-size')))


def read_score():
    partite = 0
    tempo = 0
    score1 = 0
    score2 = 0
    x1 = 0
    x2 = 0
    i = 0
    for elm in x.find_elements_by_css_selector(".middle-label,.home,.away"):

        if i==0:
            partite = partite +1
            tempo = elm.text
        elif i==1:
            score1 = elm.text
        elif i==2:
            score2 = elm.text
        if i <2:
            i=i+1
        else:
            i = 0
            x1 = x.find_elements_by_xpath('//*/div[@class=".middle-label"]/following-sibling::div[@class="bet-button-price"]')
            x2 = x.find_elements_by_xpath('//*/div[@class=".middle-label"]/following-sibling::div[@class="bet-button-size"]')
            print("Partita #" + str(partite) + " - Tempo: " + str(tempo) + " - Risultato: [" + str(score1) + " - " + str(score2) + "] - Quotes: " + str(x1) + " - " + str(x2))
            print("-----------")

            print(str(x1) + " - " + str(x2))




count = 0
while count == 0:
    #try:
        sleep(5) 
        read_score()
        sleep(10) 
        x.get('https://www.betfair.it/exchange/plus/football/inplay')
        sleep(5)
    #except Exception:
        #print("Error")

对不起,我的英语不好

0 个答案:

没有答案