【问题标题】:how to get height and 'X' value from react tag using BeautifulSoup?如何使用 BeautifulSoup 从反应标签中获取高度和 \'X\' 值?
【发布时间】:2022-12-20 07:50:22
【问题描述】:

我正在尝试从该站点获取条形图高度和 x 值 https://www.sofascore.com/football/livescore

这是我想要做的:

这是我得到的

这是我的代码

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()`

【问题讨论】:

    标签: python selenium beautifulsoup


    【解决方案1】:

    有一个 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
    

    【讨论】:

    • 我将如何获得此事件 ID?
    • 伙计看到代码并告诉我我已经设法获取数据现在在获取值时遇到问题
    • 开发工具 (shft-ctrl-i)。查看网络 -> XHR
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-23
    • 2017-06-29
    • 1970-01-01
    • 2016-10-31
    • 1970-01-01
    相关资源
    最近更新 更多