Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
514 views
in Technique[技术] by (71.8m points)

python - json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) error while scraping data from understat.com

I am trying to scrape data of a match played between United and Sheffield United yesterday night in the premier league from understat.com. My goal is to fetch "shots per game". If you see understat.com, it has a match id for all the matches and I am using that match id to scrape the data using BS4 and requests. I have successfully located the class and got the raw data that I need to fetch in JSON format but it's giving me an error like "json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)". Below is my code:

#Import packages and modules
import requests
from bs4 import BeautifulSoup
import json
import pandas as pd

#Set up the url and class to scrape
#scrape the single game shot
base_url = "https://understat.com/match/"

match_id = str(input('Please enter the match id: '))
url = base_url + match_id

print(url)

res = requests.get(url)
soup = BeautifulSoup(res.content, 'lxml')
scripts = soup.find_all('script')

print(scripts)

#only the shot data
strings = scripts[1].string
print(strings)

#strip symbols so we only have the json data
index_start = strings.index("('")+1
index_end = strings.index("')")

json_data = strings[index_start:index_end]
json_data = json_data.encode('utf8').decode('unicode_escape')

#convert string to json format
data = json.loads(json_data)
print(data)

Shots strings that I am trying to fetch: var shotsData = JSON.parse('x7Bx22hx22x3Ax5Bx7Bx22idx22x3Ax22401340x22,x22minutex22x3Ax2210x22,x22resultx22x3Ax22MissedShotsx22,x22Xx22x3Ax220.8390000152587891x22,x22Yx22x3Ax220.5379999923706055x22,x22xGx22x3Ax220.08287161588668823x22,x22playerx22x3Ax22Marcusx20Rashfordx22,x22h_ax22x3Ax22hx22,x22player_idx22x3Ax22556x22,x22situationx22x3Ax22OpenPlayx22,x22seasonx22x3Ax222020x22,x22shotTypex22x3Ax22RightFootx22,x22match_idx22x3Ax2214628x22,x22h_teamx22x3Ax22Manchesterx20Unitedx22,x22a_teamx22x3Ax22Sheffieldx20Unitedx22,x22h_goalsx22x3Ax221x22,x22a_goalsx22x3Ax222x22,x22datex22x3Ax222021x2D01x2D27x2020x3A15x3A00x22,x22player_assistedx22x3Ax22Aaronx20Wanx2DBissakax22,x22lastActionx22x3Ax22Passx22x7D,x7Bx22idx22x3Ax22401342x22,x22minutex22x3Ax2216x22,x22resultx22x3Ax22BlockedShotx22,x22Xx22x3Ax220.9230000305175782x22,x22Yx22x3Ax220.705999984741211x22,x22xGx22x3Ax220.04452449828386307x22,x22playerx22x3Ax22Masonx20Greenwoodx22,x22h_ax22x3Ax22hx22,x22player_idx22x3Ax227490x22,x22situationx22x3Ax22OpenPlayx22,x22seasonx22x3Ax222020x22,x22shotTypex22x3Ax22LeftFootx22,x22match_idx22x3Ax2214628x22,x22h_teamx22x3Ax22Manchesterx20Unitedx22,x22a_teamx22x3Ax22Sheffieldx20Unitedx22,x22h_goalsx22x3Ax221x22,x22a_goalsx22x3Ax222x22,x22datex22x3Ax222021x2D01x2D27x2020x3A15x3A00x22,x22player_assistedx22x3Ax22Brunox20Fernandesx22,x22lastActionx22x3Ax22Chippedx22x7D,x7Bx22idx22x3Ax22401344x22,x22minutex22x3Ax2226x22,x22resultx22x3Ax22SavedShotx22,x22Xx22x3Ax220.74x22,x22Yx22x3Ax220.37900001525878907x22,x22xGx22x3Ax220.019290726631879807x22,x22playerx22x3Ax22Masonx20Greenwoodx22,x22h_ax22x3Ax22hx22,x22player_idx22x3Ax227490x22,x22situationx22x3Ax22OpenPlayx22,x22seasonx22x3Ax222020x22,x22shotTypex22x3Ax22LeftFootx22,x22match_idx22x3Ax2214628x22,x22h_teamx22x3Ax22Manchesterx20Unitedx22,x22a_teamx22x3Ax22Sheffieldx20Unitedx22,x22h_goalsx22x3Ax221x22,x22a_goalsx22x3Ax222x22,x22datex22x3Ax222021x2D01x2D27x2020x3A15x3A00x22,x22player_assistedx22x3Ax22Aaronx20Wanx2DBissakax22,x22lastActionx22x3Ax22Passx22x7D,x7Bx22idx22x3Ax22401345x22,x22minutex22x3Ax2227x22,x22resultx22x3Ax22BlockedShotx22,x22Xx22x3Ax220.8930000305175781x22,x22Yx22x3Ax220.34900001525878904x22,x22xGx22x3Ax220.07055725157260895x22,x22playerx22x3Ax22Masonx20Greenwoodx22,x22h_ax22x3Ax22hx22,x22player_idx22x3Ax227490x22,x22situationx22x3Ax22OpenPlayx22,x22seasonx22x3Ax222020x22,x22shotTypex22x3Ax22RightFootx22,x22match_idx22x3Ax2214628x22,x22h_teamx22x3Ax22Manchesterx20Unitedx22,x22a_teamx22x3Ax22Sheffieldx20Unitedx22,x22h_goalsx22x3Ax221x22,x22a_goalsx22x3Ax222x22,x22datex22x3Ax222021x2D01x2D27x2020x3A15x3A00x22,x22player_assistedx22x3Ax22Aaronx20Wanx2DBissakax22,x22lastActionx22x3Ax22Passx22x7D,x7Bx22idx22x3Ax22401346x22,x22minutex22x3Ax2229x22,x22resultx22x3Ax22MissedShotsx22,x22Xx22x3Ax220.919000015258789x22,x22Yx22x3Ax220.46599998474121096x22,x22xGx22x3Ax220.04169069603085518x22,x22playerx22x3Ax22Harryx20Maguirex22,x22h_ax22x3Ax22hx22,x22player_idx22x3Ax221687x22,x22situationx22x3Ax22FromCornerx22,x22seasonx22x3Ax222020x22,x22shotTypex22x3Ax22Headx22,x22match_idx22x3Ax2214628x22,x22h_teamx22x3Ax22Manchesterx20Unitedx22,x22a_teamx22x3Ax22Sheffieldx20Unitedx22,x22h_goalsx22x3Ax221x22,x22a_goalsx22x3Ax222x22,x22datex22x3Ax222021x2D01x2D27x2020x3A15x3A00x22,x22player_assistedx22x3Anull,x22lastActionx22x3Ax22Foulx22x7D,x7Bx22idx22x3Ax22401347x22,x22minutex22x3Ax2238x22,x22resultx22x3Ax22BlockedShotx22,x22Xx22x3Ax220.7169999694824218x22,x22Yx22x3Ax220.4370000076293945x22,x22xGx22x3Ax220.016049593687057495x22,x22playerx22x3Ax22Brunox20Fernandesx22,x22h_ax22x3Ax22hx22,x22player_idx22x3Ax221228x22,x22situationx22x3Ax22OpenPlayx22,x22seasonx22x3Ax222020x22,x22shotTypex22x3Ax22RightFootx22,x22match_idx22x3Ax2214628x22,x22h_teamx22x3Ax22Manchesterx20Unitedx22,x22a_teamx22x3Ax22Sheffieldx20Unitedx22,x22h_goalsx22x3Ax221x22,x22a_goalsx22x3Ax222x22,x22datex22x3Ax222021x2D01x2D27x2020x3A15x3A00x22,x22player_assistedx22x3Ax22Paulx20Pogbax22,x22lastActionx22x3Ax22Passx22x7D,x7Bx22idx22x3Ax22401348x22,x22minutex22x3Ax2240x22,x22resultx22x3Ax22SavedShotx22,x22Xx22x3Ax220.9330000305175781x22,x22Yx22x3Ax220.485x22,x22xGx22x3Ax220.0721256285905838x22,x22playerx22x3Ax22Anthonyx20Martialx22,x22h_ax22x3Ax22hx22,x22player_idx22x3Ax22553x22,x22situationx22x3Ax22OpenPlayx22,x22seasonx22x3Ax222020x22,x22shotTypex22x3Ax22Headx22,x22match_idx22x3Ax2214628x22,x22h_teamx22x3Ax22Manchesterx20Unitedx22,x22a_teamx22x3Ax22Sheffieldx20Unitedx22,x22h_goalsx22x3Ax221x22,x22a_goalsx22x3Ax222x22,x22datex22x3Ax222021x2D01x2D27x2020x3A15x3A00x22,x22player_assistedx22x3Ax22Brunox20Fernandesx22,x22lastActionx22x3Ax22Passx22x7D,x7Bx22idx22x3Ax22401349x22,x22minutex22x3Ax2248x22,x22resultx22x3Ax22MissedShotsx22,x22Xx22x3Ax220.925x22,x22Yx22x3Ax220.35700000762939454x22,x22xGx22x3Ax220.27813461422920227x22,x22playerx22x3Ax22Masonx20Greenwoodx22,x22h_ax22x3Ax22hx22,x22player_idx22x3Ax227490x22,x22situationx22x3Ax22OpenPlayx22,x22seasonx22x3Ax222020x22,x22shotTypex22x3Ax22RightFootx22,x22match_idx22x3Ax2214628x22,x22h_teamx22x3Ax22Manchesterx20Unitedx22,x22a_teamx22x3Ax22Sheffieldx20Unitedx22,x22h_goalsx22x3Ax221x22,x22a_goalsx22x3Ax222x22,x22datex22x3Ax222021x2D01x2D27x2020x3A15x3A00x22,x22player_assistedx22x3Ax22Brunox20Fernandesx22,x22lastActionx22x3Ax22Passx22x7D,x7Bx22idx22x3Ax22401350x22,x22minutex22x3Ax2251x22,x22resultx22x3Ax22BlockedShotx22,x22Xx22x3Ax220.769000015258789x22,x22Yx22x3Ax220.35200000762939454x22,x22xGx22x3Ax220.019760465249419212x22,x22playerx22x3Ax22Aaronx20Wanx2DBissakax22,x22h_ax22x3Ax22hx22,x22player_idx22x3Ax225584x22,x22situationx22x3Ax22OpenPlayx22,x22seasonx22x3Ax222020x22,x22shotTypex22x3Ax22RightFootx22,x22match_idx22x3Ax2214628x22,x22h_teamx22x3Ax22Manchesterx20Unitedx22,x22a_teamx22x3Ax22Sheffieldx20Unitedx22,x22h_goalsx22x3Ax221x22,x22a_goalsx22x3Ax222x22,x22datex22x3Ax222021x2D01x2D27x2020x3A15x3A00x22,x22player_assistedx22x3Anull,x22lastActionx22x3Ax22Nonex22x7D,x7Bx22idx22x3Ax22401352x22,x22minutex22x3Ax2261x22,x22resultx22x3Ax22SavedShotx22,x22Xx22x3Ax220.6830000305175781x22,x22Yx22x3Ax220.5070000076293946x22,x22xGx22x3Ax220.032827332615852356x22,x22playerx22x3Ax22Marcusx20Rashfordx22,x22h_ax22x3Ax22hx22,x22player_idx22x3Ax22556x22,x22situationx22x3Ax22DirectFreekickx22,x22seasonx22x3Ax222020x22,x22shotTypex22x3Ax22RightFootx22,x22match_idx22x3Ax2214628x22,x22h_teamx22x3Ax22Manchesterx20Unitedx22,x22a_teamx22x3Ax22Sheffieldx20Unitedx22,x22h_goalsx22x3Ax221x22,x22a_goalsx22x3Ax222x22,x22datex22x3Ax222021x2D01x2D27x2020x3A15x3A00x22,x22player_assistedx22x3Anull,x22lastActionx22x3Ax22Standardx22x7D,x7Bx22idx22x3Ax22401353x22,x22minutex22x3Ax2263x22,x22resultx22x3Ax22BlockedShotx22,x22Xx22x3Ax220.77x22,x22Yx22x3Ax220.6519999694824219x22,x22xGx22x3Ax220.061657458543777466x22,x22playerx22x3Ax22Brunox20Fernandesx22,x22h_ax22x3Ax22hx22,x22player_idx22x3Ax221228x22,x22situationx22x3Ax22DirectFreekickx22,x22seasonx22x3Ax222020x22,x22shotTypex22x3Ax22RightFootx22,x22match_idx22x3Ax2214628x22,x22h_teamx22x3Ax22Manchesterx20Unitedx22,x22a_teamx22x3Ax22Sheffieldx20Unitedx22,x22h_goalsx22x3Ax221x22,x22a_goalsx22x3Ax222x22,x22datex22x3Ax222021x2D01x2D27x2020x3A15x3A00x22,x22player_assistedx22x3Anull,x22lastActionx22x3Ax22Standardx22x7D,x7Bx22idx22x3Ax22401354x22,x22minutex22x3Ax2263x22,x22resultx22x3Ax22Goalx22,x22Xx22x3Ax220.9180000305175782x22,x22Yx22x3Ax220.51x22,x22xGx22x3Ax220.04361902177333832x22,x22playerx22x3Ax22Harryx20Maguirex22,x22h_ax22x3Ax22hx22,x22player_idx22x3Ax221687x22,x22situationx22x3Ax22FromCornerx22,x22seasonx22x3Ax222020x22,x22shotTypex22x3Ax22Headx22,x22match_idx22x3Ax2214628x22,x22h_teamx22x3Ax22Manchesterx20Unitedx22,x22a_teamx22x3Ax22Sheffieldx20Unitedx22,x22h_goalsx22x3Ax221x22,x22a_goalsx22x3Ax222x22,x22datex22x3Ax222021x2D01x2D27x2020x3A15x3A00x22,x22player_assistedx22x3Ax22Alexx20Tellesx22,x22lastActionx22x3Ax22Aerialx22x7D,x7Bx22idx22x3Ax22401357x22,x22minutex22x3Ax2276x22,x22resultx22x3Ax22BlockedShotx22,x22Xx22x3Ax220.7609999847412109x22,x22Yx22x3Ax220.6780000305175782x22,x22xGx22x3Ax220.016254646703600883x22,x22playerx22x3Ax22Alexx20Tellesx22,x22h_ax22x3Ax22hx22,x22player_idx22x3Ax221828x22,x22situationx22x3Ax22OpenPlayx22,x22seasonx22x3Ax222020x22,x22shotTypex22x3Ax22LeftFootx22,x22match_idx22x3Ax2214628x22,x22h_teamx22x3Ax22Manchesterx20Unitedx22,x22a_teamx22x3Ax22Sheffieldx20Unitedx22,x22h_goalsx22x3Ax221x22,x22a_goalsx22x3Ax222x22,x22datex22x3Ax222021x2D01x2D27x2020x3A15x3A00x22,x22player_assistedx22x3Anull,x22lastActionx22x3Ax22Nonex22x7D,x7Bx22idx22x3Ax22401358x22,x22minutex22x3Ax2276x22,x22resultx22x3Ax22MissedShotsx22,x22Xx22x3Ax220.924000015258789x22,x22Yx22x3Ax220.5070000076293946x22,x22xGx22x3Ax220.10926949232816696x22,x22playerx22x3Ax22Paulx20Pogbax22,x22h_ax22x3Ax22hx22,x22player_idx22x3Ax221740x22,x22situationx22x3Ax22FromCornerx22,x22seasonx22x3Ax222020x22,x22shotTypex22x3Ax22Headx22,x22match_idx22x3Ax2214628x22,x22h_teamx22x3Ax22Manchesterx20Unitedx22,x22a_teamx22x3Ax22Sheffieldx20Unitedx22,x22h_goalsx22x3Ax221x22,x22a_goalsx22x3Ax222x22,x22datex22x3Ax222021x2D01x2D27x2020x3A15x3A00x22,x22player_assistedx22x3Ax22Alexx20Tellesx22,x22lastActionx22x3Ax22Crossx22x7D,x7Bx22idx22x3Ax22401359x22,x22minutex22x3Ax2281x22,x22resultx22x3Ax22MissedShotsx22,x22Xx22x3Ax220.8630000305175781x22,x22Yx22x3Ax220.5570000076293945x22,x22xGx22x3Ax220.06367684155702591x22,x22playerx22x3Ax22Edinsonx20Cavanix22,x22h_ax22x3Ax22hx22,x22player_idx22x3Ax223294x22,x22situationx22x3Ax22OpenPlayx22,x22seasonx22x3Ax222020x22,x22shotTypex22x3Ax22LeftFootx22,x22match_idx22x3Ax2214628x22,x22h_teamx22x3Ax22Manchesterx20Unitedx22,x22a_teamx22x3Ax22Sheffieldx20Unitedx22,x22h_goalsx22x3Ax221x22,x22a_goalsx22x3Ax222x22,x22datex22x3Ax222021x2D01x2D27x2020x3A15x3A00x22,x22player_assistedx22x3Anull,x22lastActionx22x3Ax22Nonex22x7D,x7Bx22idx22x3Ax22401360x22,x22minutex22x3Ax2285x22,x22resultx22x3Ax22BlockedShotx22,x22Xx22x3Ax220.86x22,x22Yx22x3Ax220.775x22,x22xGx22x3Ax220.02079056017100811x22,x22playerx22x3Ax22Brunox20Fernandesx22,x22h_ax22x3Ax22hx22,x22player_idx22x3Ax221228x22,x22situationx22x3Ax22FromCornerx22,x22seasonx22x3Ax222020x22,x22shotTypex22x3Ax22RightFootx22,x22match_idx22x3Ax2214628x22,x22h_teamx22x3Ax22Manchesterx20Unitedx22,x22a_teamx22x3Ax22Sheffieldx20Unitedx22,x22h_goalsx22x3Ax221x22,x22a_goalsx22x3Ax222x22,x22datex22x3Ax222021x2D01x2D27x2020x3A15x3A00x22,x22player_assistedx22x3Ax22Lukex20Shawx22,x22lastActionx22x3Ax22Passx22x7Dx5D,x22ax22x3Ax5Bx7Bx22idx22x3Ax22401341x22,x22minutex22x3Ax2215x22,x22resultx22x3Ax22SavedShotx22,x22Xx22x3Ax220.8690000152587891x22,x22Y


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The problem is your json_data as a string starts with the '{. The start index you want is actually one more index value ahead at the {, so you want to add 2, not 1 to the index start:

index_start = strings.index("('")+2 instead of index_start = strings.index("('")+1

#Import packages and modules
import requests
from bs4 import BeautifulSoup
import json
import pandas as pd

#Set up the url and class to scrape
#scrape the single game shot
base_url = "https://understat.com/match/"

match_id = str(input('Please enter the match id: '))
#match_id = '14628'
url = base_url + match_id

print(url)

res = requests.get(url)
soup = BeautifulSoup(res.content, 'lxml')
scripts = soup.find_all('script')

print(scripts)

#only the shot data
strings = scripts[1].string
print(strings)

#strip symbols so we only have the json data
index_start = strings.index("('")+2   # <--- CHANGED HERE
index_end = strings.index("')")

json_data = strings[index_start:index_end]
json_data = json_data.encode('utf8').decode('unicode_escape')

#convert string to json format
data = json.loads(json_data)
print(data)

Output:

{'h': [{'id': '401340', 'minute': '10', 'result': 'MissedShots', 'X': '0.8390000152587891', 'Y': '0.5379999923706055', 'xG': '0.08287161588668823', 'player': 'Marcus Rashford', 'h_a': 'h', 'player_id': '556', 'situation': 'OpenPlay', 'season': '2020', 'shotType': 'RightFoot', 'match_id': '14628', 'h_team': 'Manchester United', 'a_team': 'Sheffield United', 'h_goals': '1', 'a_goals': '2', 'date': '2021-01-27 20:15:00', 'player_assisted': 'Aaron Wan-Bissaka', 'lastAction': 'Pass'}, {'id': '401342', 'minute': '16', 'result': 'BlockedShot', 'X': '0.9230000305175782', 'Y': '0.705999984741211', 'xG': '0.04452449828386307', 'player': 'Mason Greenwood', 'h_a': 'h', 'player_id': '7490', 'situation': 'OpenPlay', 'season': '2020', 'shotType': 'LeftFoot', 'match_id': '14628', 'h_team': 'Manchester United', 'a_team': 'Sheffield United', 'h_goals': '1', 'a_goals': '2', 'date': '2021-01-27 20:15:00', 'player_assisted': 'Bruno Fernandes', 'lastAction': 'Chipped'}, {'id': '401344', 'minute': '26', 'result': 'SavedShot', 'X': '0.74', 'Y': '0.37900001525878907', 'xG': '0.019290726631879807', 'player': 'Mason Greenwood', 'h_a': 'h', 'player_id': '7490', 'situation': 'OpenPlay', 'season': '2020', 'shotType': 'LeftFoot', 'match_id': '14628', 'h_team': 'Manchester United', 'a_team': 'Sheffield United', 'h_goals': '1', 'a_goals': '2', 'date': '2021-01-27 20:15:00', 'player_assisted': 'Aaron Wan-Bissaka', 'lastAction': 'Pass'}, {'id': '401345', 'minute': '27', 'result': 'BlockedShot', 'X': '0.8930000305175781', 'Y': '0.34900001525878904', 'xG': '0.07055725157260895', 'player': 'Mason Greenwood', 'h_a': 'h', 'player_id': '7490', 'situation': 'OpenPlay', 'season': '2020', 'shotType': 'RightFoot', 'match_id': '14628', 'h_team': 'Manchester United', 'a_team': 'Sheffield United', 'h_goals': '1', 'a_goals': '2', 'date': '2021-01-27 20:15:00', 'player_assisted': 'Aaron Wan-Bissaka', 'lastAction': 'Pass'}, {'id': '401346', 'minute': '29', 'result': 'MissedShots', 'X': '0.919000015258789', 'Y': '0.46599998474121096', 'xG': '0.04169069603085518', 'player': 'Harry Maguire', 'h_a': 'h', 'player_id': '1687', 'situation': 'FromCorner', 'season': '2020', 'shotType': 'Head', 'match_id': '14628', 'h_team': 'Manchester United', 'a_team': 'Sheffield United', 'h_goals': '1', 'a_goals': '2', 'date': '2021-01-27 20:15:00', 'player_assisted': None, 'lastAction': 'Foul'}, {'id': '401347', 'minute': '38', 'result': 'BlockedShot', 'X': '0.7169999694824218', 'Y': '0.4370000076293945', 'xG': '0.016049593687057495', 'player': 'Bruno Fernandes', 'h_a': 'h', 'player_id': '1228', 'situation': 'OpenPlay', 'season': '2020', 'shotType': 'RightFoot', 'match_id': '14628', 'h_team': 'Manchester United', 'a_team': 'Sheffield United', 'h_goals': '1', 'a_goals': '2', 'date': '2021-01-27 20:15:00', 'player_assisted': 'Paul Pogba', 'lastAction': 'Pass'}, {'id': '401348', 'minute': '40', 'result': 'SavedShot', 'X': '0.9330000305175781', 'Y': '0.485', 'xG': '0.0721256285905838', 'player': 'Anthony Martial', 'h_a': 'h', 'player_id': '553', 'situation': 'OpenPlay', 'season': '2020', 'shotType': 'Head', 'match_id': '14628', 'h_team': 'Manchester United', 'a_team': 'Sheffield United', 'h_goals': '1', 'a_goals': '2', 'date': '2021-01-27 20:15:00', 'player_assisted': 'Bruno Fernandes', 'lastAction': 'Pass'}, {'id': '401349', 'minute': '48', 'result': 'MissedShots', 'X': '0.925', 'Y': '0.35700000762939454', 'xG': '0.27813461422920227', 'player': 'Mason Greenwood', 'h_a': 'h', 'player_id': '7490', 'situation': 'OpenPlay', 'season': '2020', 'shotType': 'RightFoot', 'match_id': '14628', 'h_team': 'Manchester United', 'a_team': 'Sheffield United', 'h_goals': '1', 'a_goals': '2', 'date': '2021-01-27 20:15:00', 'player_assisted': 'Bruno Fernandes', 'lastAction': 'Pass'}, {'id': '401350', 'minute': '51', 'result': 'BlockedShot', 'X': '0.769000015258789', 'Y': '0.35200000762939454', 'xG': '0.019760465249419212', 'player': 'Aaron Wan-Bissaka', 'h_a': 'h', 'player_id': '5584', 'situation': 'OpenPlay', 'season': '2020', 'shotType': 'RightFoot', 'match_id': '14628', 'h_team': 'Manchester United', 'a_team': 'Sheffield United', 'h_goals': '1', 'a_goals': '2', 'date': '2021-01-27 20:15:00', 'player_assisted': None, 'lastAction': 'None'}, {'id': '401352', 'minute': '61', 'result': 'SavedShot', 'X': '0.6830000305175781', 'Y': '0.5070000076293946', 'xG': '0.032827332615852356', 'player': 'Marcus Rashford', 'h_a': 'h', 'player_id': '556', 'situation': 'DirectFreekick', 'season': '2020', 'shotType': 'RightFoot', 'match_id': '14628', 'h_team': 'Manchester United', 'a_team': 'Sheffield United', 'h_goals': '1', 'a_goals': '2', 'date': '2021-01-27 20:15:00', 'player_assisted': None, 'lastAction': 'Standard'}, {'id': '401353', 'minute': '63', 'result': 'BlockedShot', 'X': '0.77', 'Y': '0.6519999694824219', 'xG': '0.061657458543777466', 'player': 'Bruno Fernandes', 'h_a': 'h', 'player_id': '1228', 'situation': 'DirectFreekick', 'season': '2020', 'shotType': 'RightFoot', 'match_id': '14628', 'h_team': 'Manchester United', 'a_team': 'Sheffield United', 'h_goals': '1', 'a_goals': '2', 'date': '2021-01-27 20:15:00', 'player_assisted': None, 'lastAction': 'Standard'}, {'id': '401354', 'minute': '63', 'result': 'Goal', 'X': '0.9180000305175782', 'Y': '0.51', 'xG': '0.04361902177333832', 'player': 'Harry Maguire', 'h_a': 'h', 'player_id': '1687', 'situation': 'FromCorner', 'season': '2020', 'shotType': 'Head', 'match_id': '14628', 'h_team': 'Manchester United', 'a_team': 'Sheffield United', 'h_goals': '1', 'a_goals': '2', 'date': '2021-01-27 20:15:00', 'player_assisted': 'Alex Telles', 'lastAction': 'Aerial'}, {'id': '401357', 'minute': '76', 'result': 'BlockedShot', 'X': '0.7609999847412109', 'Y': '0.6780000305175782', 'xG': '0.016254646703600883', 'player': 'Alex Telles', 'h_a': 'h', 'player_id': '1828', 'situation': 'OpenPlay', 'season': '2020', 'shotType': 'LeftFoot', 'match_id': '14628', 'h_team': 'Manchester United', 'a_team': 'Sheffield United', 'h_goals': '1', 'a_goals': '2', 'date': '2021-01-27 20:15:00', 'player_assisted': None, 'lastAction': 'None'}, {'id': '401358', 'minute': '76', 'result': 'MissedShots', 'X': '0.924000015258789', 'Y': '0.5070000076293946', 'xG': '0.10926949232816696', 'player': 'Paul Pogba', 'h_a': 'h', 'player_id': '1740', 'situation': 'FromCorner', 'season': '2020', 'shotType': 'Head', 'match_id': '14628', 'h_team': 'Manchester United', 'a_team': 'Sheffield United', 'h_goals': '1', 'a_goals': '2', 'date': '2021-01-27 20:15:00', 'player_assisted': 'Alex Telles', 'lastAction': 'Cross'}, {'id': '401359', 'minute': '81', 'result': 'MissedShots', 'X': '0.8630000305175781', 'Y': '0.5570000076293945', 'xG': '0.06367684155702591', 'player': 'Edinson Cavani', 'h_a': 'h', 'player_id': '3294', 'situation': 'OpenPlay', 'season': '2020', 'shotType': 'LeftFoot', 'match_id': '14628', 'h_team': 'Manchester United', 'a_team': 'Sheffield United', 'h_goals': '1', 'a_goals': '2', 'date': '2021-01-27 20:15:00', 'player_assisted': None, 'lastAction': 'None'}, {'id': '401360', 'minute': '85', 'result': 'BlockedShot', 'X': '0.86', 'Y': '0.775', 'xG': '0.02079056017100811', 'player': 'Bruno Fernandes', 'h_a': 'h', 'player_id': '1228', 'situation': 'FromCorner', 'season': '2020', 'shotType': 'RightFoot', 'match_id': '14628', 'h_team': 'Manchester United', 'a_team': 'Sheffield United', 'h_goals': '1', 'a_goals': '2', 'date': '2021-01-27 20:15:00', 'player_assisted': 'Luke Shaw', 'lastAction': 'Pass'}], 'a': [{'id': '401341', 'minute': '15', 'result': 'SavedShot', 'X': '0.8690000152587891', 'Y': '0.5479999923706055', 'xG': '0.12690183520317078', 'player': 'Billy Sharp', 'h_a': 'a', 'player_id': '7712', 'situation': 'OpenPlay', 'season': '2020', 'shotType': 'LeftFoot', 'match_id': '14628', 'h_team': 'Manchester United', 'a_team': 'Sheffield United', 'h_goals': '1', 'a_goals': '2', 'date': '2021-01-27 20:15:00', 'player_assisted': 'John Fleck', 'lastAction': 'Throughball'}, {'id': '401343', 'minute': '22', 'result': 'Goal', 'X': '0.98', 'Y': '0.49400001525878906', 'xG': '0.15722613036632538', 'player': 'Kean Bryan', 'h_a': 'a', 'player_id': '9163', 'situation': 'FromCorner', 'season': '2020', 'shotType': 'Head', 'match_id': '14628', 'h_team': 'Manchester United', 'a_team': 'Sheffield United', 'h_goals': '1', 'a_goals': '2', 'date': '2021-01-27 20:15:00', 'player_assisted': 'John Fleck', 'lastAction': 'Cross'}, {'id': '401351', 'minute': '55', 'result': 'MissedShots', 'X': '0.8430000305175781', 'Y': '0.29600000381469727', 'xG': '0.0439084991812706', 'player': 'David McGoldrick', 'h_a': 'a', 'player_id': '7711', 'situation': 'OpenPlay', 'season': '2020', 'shotType': 'RightFoot', 'match_id': '14628', 'h_team': 'Manchester United', 'a_team': 'Sheffield United', 'h_goals': '1', 'a_goals': '2', 'date': '2021-01-27 20:15:00', 'player_assisted': 'George Baldock', 'lastAction': 'Pass'}, {'id': '401355', 'minute': '73', 'result': 'BlockedShot', 'X': '0.875', 'Y': '0.3609999847412109', 'xG': '0.07747054845094681', 'player': 'Oliver Burke', 'h_a': 'a', 'player_id': '5256', 'situation': 'OpenPlay', 'season': '2020', 'shotType': 'RightFoot', 'match_id': '14628', 'h_team': 'Manchester United', 'a_team': 'Sheffield United', 'h_goals': '1', 'a_goals': '2', 'date': '2021-01-27 20:15:00', 'player_assisted': 'John Lundstram', 'lastAction': 'Pass'}, {'id': '401356', 'minute': '73', 'result': 'Goal', 'X': '0.8769999694824219', 'Y': '0.3079999923706055', 'xG': '0.28404054045677185', 'player': 'Oliver Burke', 'h_a': 'a', 'player_id': '5256', 'situation': 'OpenPlay', 'season': '2020', 'shotType': 'RightFoot', 'match_id': '14628', 'h_team': 'Manchester United', 'a_team': 'Sheffield United', 'h_goals': '1', 'a_goals': '2', 'date': '2021-01-27 20:15:00', 'player_assisted': 'John Lundstram', 'lastAction': 'Pass'}]}

Also you could replace:

#strip symbols so we only have the json data
index_start = strings.index("('")+2   # <--- CHANGED HERE
index_end = strings.index("')")

json_data = strings[index_start:index_end]

with:

#strip symbols so we only have the json data
json_data = strings.split("('", 1)[-1].rsplit("')",2)[0]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...