Skip to content
Advertisement

how to get height and ‘X’ value from react tag using BeautifulSoup?

I am trying to get the bar graph height and value of x from this site https://www.sofascore.com/football/livescore

Here is what I want to do:
Here is what I want to do

Here is what I am getting
my Output

here is my code

import time
from selenium import webdriver
from bs4 import BeautifulSoup
def scrape(url):
    print("n" + url)
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_experimental_option('useAutomationExtension', False)
    driver = webdriver.Chrome("E:chromedriver.exe", options=chrome_options)
    driver.maximize_window()
    driver.get(url)
    time.sleep(1)
    get_data = driver.find_element_by_xpath('//*[@id="__next"]/main/div/div[2]/div/div[3]/div[2]/div/div/div/div/div[2]/a/div/div')
    driver.execute_script("arguments[0].click();", get_data)
    time.sleep(3)
    soup=BeautifulSoup(driver.page_source, 'lxml')
    Graph = soup.find_all('g', {'class': 'bars-group'})
    for a in Graph:
        print(str(a),"n")
def main():
    print("In main")
    scrape(url="https://www.sofascore.com/football/livescore")
if __name__ == "__main__":
    main()`

Advertisement

Answer

There’s an api to get that. Just input the event ID.

import requests
import pandas as pd

eventId = 10181868
url = f'https://api.sofascore.com/api/v1/event/{eventId}/graph'
jsonData = requests.get(url).json()

df = pd.DataFrame(jsonData['graphPoints'])

Output:

print(df)
    minute  value
0      1.0      5
1      2.0     -6
2      3.0     -7
3      4.0    -19
4      5.0     -6
5      6.0     -5
6      7.0     15
7      8.0      1
8      9.0     -6
9     10.0     -2
10    11.0     32
11    12.0     60
12    13.0     49
13    14.0     32
14    15.0     30
15    16.0     18
16    17.0     10
17    18.0     53
18    19.0     18
19    20.0     17
20    21.0     44
21    22.0     24
22    23.0      5
23    24.0     -6
24    25.0      3
25    26.0     -9
26    27.0    -61
27    28.0    -43
28    29.0    -45
29    30.0    -45
30    31.0    -27
31    32.0    -22
32    33.0    -13
33    34.0     -7
34    35.0     20
35    36.0     35
36    37.0     15
37    38.0      3
38    39.0    -48
39    40.0    -21
40    41.0    -13
41    42.0     13
42    43.0     -9
43    44.0     -5
44    45.0     -4
45    45.5     33
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement