如何使用 BeautifulSoup 从反应标签中获取高度和 'X' 值?
how to get height and 'X' value from react tag using BeautifulSoap?
您好,我正在尝试从此站点获取条形图高度和 x 值
https://www.sofascore.com/football/livescore
这是我想做的
Here is what I want to do
这是我得到的
my Output
这是我的代码`
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()`
提前致谢:)
有一个 api 可以做到这一点。只需输入事件 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'])
输出:
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
您好,我正在尝试从此站点获取条形图高度和 x 值
https://www.sofascore.com/football/livescore
这是我想做的
Here is what I want to do
这是我得到的 my Output 这是我的代码`
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()`
提前致谢:)
有一个 api 可以做到这一点。只需输入事件 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'])
输出:
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