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 am getting
here is my code
JavaScript
x
24
24
1
import time
2
from selenium import webdriver
3
from bs4 import BeautifulSoup
4
def scrape(url):
5
print("n" + url)
6
chrome_options = webdriver.ChromeOptions()
7
chrome_options.add_experimental_option('useAutomationExtension', False)
8
driver = webdriver.Chrome("E:chromedriver.exe", options=chrome_options)
9
driver.maximize_window()
10
driver.get(url)
11
time.sleep(1)
12
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')
13
driver.execute_script("arguments[0].click();", get_data)
14
time.sleep(3)
15
soup=BeautifulSoup(driver.page_source, 'lxml')
16
Graph = soup.find_all('g', {'class': 'bars-group'})
17
for a in Graph:
18
print(str(a),"n")
19
def main():
20
print("In main")
21
scrape(url="https://www.sofascore.com/football/livescore")
22
if __name__ == "__main__":
23
main()`
24
Advertisement
Answer
There’s an api to get that. Just input the event ID.
JavaScript
1
9
1
import requests
2
import pandas as pd
3
4
eventId = 10181868
5
url = f'https://api.sofascore.com/api/v1/event/{eventId}/graph'
6
jsonData = requests.get(url).json()
7
8
df = pd.DataFrame(jsonData['graphPoints'])
9
Output:
JavaScript
1
49
49
1
print(df)
2
minute value
3
0 1.0 5
4
1 2.0 -6
5
2 3.0 -7
6
3 4.0 -19
7
4 5.0 -6
8
5 6.0 -5
9
6 7.0 15
10
7 8.0 1
11
8 9.0 -6
12
9 10.0 -2
13
10 11.0 32
14
11 12.0 60
15
12 13.0 49
16
13 14.0 32
17
14 15.0 30
18
15 16.0 18
19
16 17.0 10
20
17 18.0 53
21
18 19.0 18
22
19 20.0 17
23
20 21.0 44
24
21 22.0 24
25
22 23.0 5
26
23 24.0 -6
27
24 25.0 3
28
25 26.0 -9
29
26 27.0 -61
30
27 28.0 -43
31
28 29.0 -45
32
29 30.0 -45
33
30 31.0 -27
34
31 32.0 -22
35
32 33.0 -13
36
33 34.0 -7
37
34 35.0 20
38
35 36.0 35
39
36 37.0 15
40
37 38.0 3
41
38 39.0 -48
42
39 40.0 -21
43
40 41.0 -13
44
41 42.0 13
45
42 43.0 -9
46
43 44.0 -5
47
44 45.0 -4
48
45 45.5 33
49