【问题标题】:json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) error while scraping data from understat.comjson.decoder.JSONDecodeError:期望值:从 understat.com 抓取数据时出现第 1 行第 1 列(字符 0)错误
【发布时间】:2021-05-02 02:19:44
【问题描述】:

我正在尝试从 understat.com 获取昨天晚上在英超联赛中曼联和谢菲尔德联队进行的比赛的数据。我的目标是获取“每场比赛的投篮次数”。如果你看到 understat.com,它有一个所有匹配的匹配 id,我正在使用该匹配 id 使用 BS4 和请求来抓取数据。我已成功找到该类并获得了我需要以 JSON 格式获取的原始数据,但它给了我一个类似“json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)”的错误。以下是我的代码:

#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)

我试图获取的镜头字符串: var shotData = JSON.parse('\x7B\x22h\x22\x3A\x5B\x7B\x22id\x22\x3A\x22401340\x22,\x22minute\x22\x3A\x2210\x22,\x22result\x22\x3A\x22MissedShots \x22,\x22X\x22\x3A\x220.8390000152587891\x22,\x22Y\x22\x3A\x220.5379999923706055\x22,\x22xG\x22\x3A\x220.0828716158\866823\x2\x22Ax22823\x2\x22 \x20Rashford\x22,\x22h_a\x22\x3A\x22h\x22,\x22player_id\x22\x3A\x22556\x22,\x22situation\x22\x3A\x22OpenPlay\x22,\x22season\x22\x3A\x222020\x22,\ x22shotType\x22\x3A\x22RightFoot\x22,\x22match_id\x22\x3A\x2214628\x22,\x22h_team\x22\x3A\x22Manchester\x20United\x22,\x22a_team\x22\x3A\x22Sheffield\x20United\x22,\x22h_ x22\x3A\x221\x22,\x22a_goals\x22\x3A\x222\x22,\x22date\x22\x3A\x222021\x2D01\x2D27\x2020\x3A15\x3A00\x22,\x22player_assisted\x22\x3A\x22Aaron\x20 \x2DBissaka\x22,\x22lastAction\x22\x3A\x22Pass\x22\x7D,\x7B\x22id\x22\x3A\x22401342\x22,\x22minute\x22\x3A\x2216\x22,\x22result\x22\x3A\x22BlockedShot \x22,\x22X\x22\x3A\x220.9230000305175782\x22,\x22Y\x22\x3A\x220.705999984741211\ x22,\x22xG\x22\x3A\x220.04452449828386307\x22,\x22player\x22\x3A\x22Mason\x20Greenwood\x22,\x22h_a\x22\x3A\x22h\x22,\x22player_id\x22\x22\x2274\x \x22situation\x22\x3A\x22OpenPlay\x22,\x22season\x22\x3A\x222020\x22,\x22shotType\x22\x3A\x22LeftFoot\x22,\x22match_id\x22\x3A\x2214628\x22,\x22h_team\x22\x3A \x22Manchester\x20United\x22,\x22a_team\x22\x3A\x22Sheffield\x20United\x22,\x22h_goals\x22\x3A\x221\x22,\x22a_goals\x22\x3A\x222\x22,\x22date\x22\x3A\x222021 \x2D01\x2D27\x2020\x3A15\x3A00\x22,\x22player_assisted\x22\x3A\x22Bruno\x20Fernandes\x22,\x22lastAction\x22\x3A\x22Chipped\x22\x7D,\x7B\x22id\x22\x3A\x224013 x22,\x22minute\x22\x3A\x2226\x22,\x22result\x22\x3A\x22SavedShot\x22,\x22X\x22\x3A\x220.74\x22,\x22Y\x22\x3A\x220.37900001525878907\x22, \x22xG\x22\x3A\x220.019290726631879807\x22,\x22player\x22\x3A\x22Mason\x20Greenwood\x22,\x22h_a\x22\x3A\x22h\x22,\x22player_id\x22\x3A\x227490\x2 \x22\x3A\x22OpenPlay\x22,\x22season\x22\x3A\x222020\x22,\x22shotType\x22\x3A\x2 2LeftFoot\x22,\x22match_id\x22\x3A\x2214628\x22,\x22h_team\x22\x3A\x22Manchester\x20United\x22,\x22a_team\x22\x3A\x22Sheffield\x20United\x22,\x22h_goals\x22\x3A\x22 x22,\x22a_goals\x22\x3A\x222\x22,\x22date\x22\x3A\x222021\x2D01\x2D27\x2020\x3A15\x3A00\x22,\x22player_assisted\x22\x3A\x22Aaron\x20Wan\x2DBissaka\x22, x22lastAction\x22\x3A\x22Pass\x22\x7D,\x7B\x22id\x22\x3A\x22401345\x22,\x22minute\x22\x3A\x2227\x22,\x22result\x22\x3A\x22BlockedShot\x22,\x22X\ X22 \ X3A \ x220.8930000305175781 \ X22,\ x22Y \ X22 \ X3A \ x220.34900001525878904 \ X22,\ x22xG \ X22 \ X3A \ x220.07055725157260895 \ X22,\ x22player \ X22 \ X3A \ x22Mason \ x20Greenwood \ X22,\ x22h_a\x22\x3A\x22h\x22,\x22player_id\x22\x3A\x227490\x22,\x22situation\x22\x3A\x22OpenPlay\x22,\x22season\x22\x3A\x222020\x22,\x22shotType\x22\x3A\ x22RightFoot\x22,\x22match_id\x22\x3A\x2214628\x22,\x22h_team\x22\x3A\x22Manchester\x20United\x22,\x22a_team\x22\x3A\x22Sheffield\x20United\x22,\x22h_goals\x22\x3A\x2 x22,\x22a_goals\x22\x3A\x222\x22,\x22date\x22\x3A\x222021\ x2D01\x2D27\x2020\x3A15\x3A00\x22,\x22player_assisted\x22\x3A\x22Aaron\x20Wan\x2DBissaka\x22,\x22lastAction\x22\x3A\x22Pass\x22\x7D,\x7B\x22id\x22\x3A\x2240 \x22,\x22minute\x22\x3A\x2229\x22,\x22result\x22\x3A\x22MissedShots\x22,\x22X\x22\x3A\x220.919000015258789\x22,\x22Y\x22\x3A\x220.46599\x220.46599\x220.46599\x2247 ,\x22xG\x22\x3A\x220.04169069603085518\x22,\x22player\x22\x3A\x22Harry\x20Maguire\x22,\x22h_a\x22\x3A\x22h\x22,\x22player_id\x22\x3A,\x22168\7x x22situation\x22\x3A\x22FromCorner\x22,\x22season\x22\x3A\x222020\x22,\x22shotType\x22\x3A\x22Head\x22,\x22match_id\x22\x3A\x2214628\x22,\x22h_team\x22\x3A\ x22Manchester\x20United\x22,\x22a_team\x22\x3A\x22Sheffield\x20United\x22,\x22h_goals\x22\x3A\x221\x22,\x22a_goals\x22\x3A\x222\x22,\x22date\x22\x3A\x222021\ x2D01\x2D27\x2020\x3A15\x3A00\x22,\x22player_assisted\x22\x3Anull,\x22lastAction\x22\x3A\x22Foul\x22\x7D,\x7B\x22id\x22\x3A\x22401347\x22,\x22minute\x22\ x3A\x2238\x22,\x22result\x22\x3A\x22BlockedShot\x22,\x22X\x22\x3A\x220.716999 9694824218 \ X22,\ x22Y \ X22 \ X3A \ x220.4370000076293945 \ X22,\ x22xG \ X22 \ X3A \ x220.016049593687057495 \ X22,\ x22player \ X22 \ X3A \ x22Bruno \ x20Fernandes \ X22,\ x22h_a \ X22 \ X3A \ x22h\x22,\x22player_id\x22\x3A\x221228\x22,\x22situation\x22\x3A\x22OpenPlay\x22,\x22season\x22\x3A\x222020\x22,\x22shotType\x22\x3A\x22RightFoot\x22,\x22match_id \x22\x3A\x2214628\x22,\x22h_team\x22\x3A\x22Manchester\x20United\x22,\x22a_team\x22\x3A\x22Sheffield\x20United\x22,\x22h_goals\x22\x3A\x221\x22,\x22a_goals\x22 \x3A\x222\x22,\x22date\x22\x3A\x222021\x2D01\x2D27\x2020\x3A15\x3A00\x22,\x22player_assisted\x22\x3A\x22Paul\x20Pogba\x22,\x22lastAction\x22\x3A\x22Pass\ x22\x7D,\x7B\x22id\x22\x3A\x22401348\x22,\x22minute\x22\x3A\x2240\x22,\x22result\x22\x3A\x22SavedShot\x22,\x22X\x22\x3A\x220.9330000305175781\ x22,\x22Y\x22\x3A\x220.485\x22,\x22xG\x22\x3A\x220.0721256285905838\x22,\x22player\x22\x3A\x22Anthony\x20Martial\x22,\x22h_a\x22\x3A\x22h\ x22,\x22player_id\x22\x3A\x22553\x22,\x22situation\x22\x3A\x22OpenPlay\x22,\x22season\x 22\x3A\x222020\x22,\x22shotType\x22\x3A\x22Head\x22,\x22match_id\x22\x3A\x2214628\x22,\x22h_team\x22\x3A\x22Manchester\x20United\x22,\x22a_team\x22\x3A\ x22Sheffield\x20United\x22,\x22h_goals\x22\x3A\x221\x22,\x22a_goals\x22\x3A\x222\x22,\x22date\x22\x3A\x222021\x2D01\x2D27\x2020\x3A15\x3A00\x22,\ x22player_assisted\x22\x3A\x22Bruno\x20Fernandes\x22,\x22lastAction\x22\x3A\x22Pass\x22\x7D,\x7B\x22id\x22\x3A\x22401349\x22,\x22minute\x22\x3A\x2248\x22,\ x22result\x22\x3A\x22MissedShots\x22,\x22X\x22\x3A\x220.925\x22,\x22Y\x22\x3A\x220.35700000762939454\x22,\x22xG\x22\x3A\x220.278134\x2142 x22player\x22\x3A\x22Mason\x20Greenwood\x22,\x22h_a\x22\x3A\x22h\x22,\x22player_id\x22\x3A\x227490\x22,\x22situation\x22\x3A\x22OpenPlay\x22,\x22season\x22\ x3A\x222020\x22,\x22shotType\x22\x3A\x22RightFoot\x22,\x22match_id\x22\x3A\x2214628\x22,\x22h_team\x22\x3A\x22Manchester\x20United\x22,\x22a_team\x22\x3A\x22Shef x20United\x22,\x22h_goals\x22\x3A\x221\x22,\x22a_goals\x22\x3A\x222\x22,\x22d吃了\x22\x3A\x222021\x2D01\x2D27\x2020\x3A15\x3A00\x22,\x22player_assisted\x22\x3A\x22Bruno\x20Fernandes\x22,\x22lastAction\x22\x3A\x22Pass\x22\x7D,\x7B\x22 \x22\x3A\x22401350\x22,\x22minute\x22\x3A\x2251\x22,\x22result\x22\x3A\x22BlockedShot\x22,\x22X\x22\x3A\x220.769000015258789\x22,\x22Y\x22\x3A \x220.35200000762939454\x22,\x22xG\x22\x3A\x220.019760465249419212\x22,\x22player\x22\x3A\x22Aaron\x20Wan\x2DBissaka\x22,\x22h_a\x22\x3A\222, \x3A\x225584\x22,\x22situation\x22\x3A\x22OpenPlay\x22,\x22season\x22\x3A\x222020\x22,\x22shotType\x22\x3A\x22RightFoot\x22,\x22match_id\x22\x3A\x2214628\x22 ,\x22h_team\x22\x3A\x22Manchester\x20United\x22,\x22a_team\x22\x3A\x22Sheffield\x20United\x22,\x22h_goals\x22\x3A\x221\x22,\x22a_goals\x22\x3A\x222\x22,\ x22date\x22\x3A\x222021\x2D01\x2D27\x2020\x3A15\x3A00\x22,\x22player_assisted\x22\x3Anull,\x22lastAction\x22\x3A\x22None\x22\x7D,\x7B\x22id\x22\x3A\x22410 \x22,\x22minute\x22\x3A\x2261\x22,\x22result\x22\x3A\x22SavedShot\x22,\ x22x \ x22 \ x3a \ x220.6830000305175781 \ x22y \ x22 \ x3a \ x220.50700 \ x220.5070000076293946860760 ,\x22h_a\x22\x3A\x22h\x22,\x22player_id\x22\x3A\x22556\x22,\x22situation\x22\x3A\x22DirectFreekick\x22,\x22season\x22\x3A\x222020\x22,\x22shotType\x22\ x3A\x22RightFoot\x22,\x22match_id\x22\x3A\x2214628\x22,\x22h_team\x22\x3A\x22Manchester\x20United\x22,\x22a_team\x22\x3A\x22Sheffield\x20United\x22,\x22h_goals\x22\xA x221\x22,\x22a_goals\x22\x3A\x222\x22,\x22date\x22\x3A\x222021\x2D01\x2D27\x2020\x3A15\x3A00\x22,\x22player_assisted\x22\x3Anull,\x22lastAction\x22\x3A\ x22Standard\x22\x7D,\x7B\x22id\x22\x3A\x22401353\x22,\x22minute\x22\x3A\x2263\x22,\x22result\x22\x3A\x22BlockedShot\x22,\x22X\x22\x3A\x220。 77\x22,\x22Y\x22\x3A\x220.6519999694824219\x22,\x22xG\x22\x3A\x220.061657458543777466\x22,\x22player\x22\x3A\x22Bruno\x20Fernandes\22\x,\Ax x22h\x22,\x22player_id\x22\x3A\x221228\x22,\x22situation\x22\x3A\x22Direc tFreekick\x22,\x22season\x22\x3A\x222020\x22,\x22shotType\x22\x3A\x22RightFoot\x22,\x22match_id\x22\x3A\x2214628\x22,\x22h_team\x22\x3A\x22Manchester\x20United\x22, \x22a_team\x22\x3A\x22Sheffield\x20United\x22,\x22h_goals\x22\x3A\x221\x22,\x22a_goals\x22\x3A\x222\x22,\x22date\x22\x3A\x222021\x2D01\x2D27\x2020\ x3A15\x3A00\x22,\x22player_assisted\x22\x3Anull,\x22lastAction\x22\x3A\x22Standard\x22\x7D,\x7B\x22id\x22\x3A\x22401354\x22,\x22minute\x22\x3A\x2263\x22, \x22结果\x22\x3A\x22目标\x22,\x22X\x22\x3A\x220.9180000305175782\x22,\x22Y\x22\x3A\x220.51\x22,\x22xG\x22\x3A\x220.0436\90217733338 \x22player\x22\x3A\x22Harry\x20Maguire\x22,\x22h_a\x22\x3A\x22h\x22,\x22player_id\x22\x3A\x221687\x22,\x22situation\x22\x3A\x22FromCorner\x22,\x22season\x22 \x3A\x222020\x22,\x22shotType\x22\x3A\x22Head\x22,\x22match_id\x22\x3A\x2214628\x22,\x22h_team\x22\x3A\x22Manchester\x20United\x22,\x22a_team\x22\x3A\x22Sheffield \x20United\x22,\x22h_goals\x22\x3A\x221\x22,\x22a_goals\x22\x3A\x222\x22,\x22da te\x22\x3A\x222021\x2D01\x2D27\x2020\x3A15\x3A00\x22,\x22player_assisted\x22\x3A\x22Alex\x20Telles\x22,\x22lastAction\x22\x3A\x22Aerial\x22\x7D,\x7B\x22id \x22\x3A\x22401357\x22,\x22minute\x22\x3A\x2276\x22,\x22result\x22\x3A\x22BlockedShot\x22,\x22X\x22\x3A\x220.7609999847412109\x22,\x22Y\x22\x3 \x220.6780000305175782\x22,\x22xG\x22\x3A\x220.016254646703600883\x22,\x22player\x22\x3A\x22Alex\x20Telles\x22,\x22h_a\x22\x3A\x22h\x22\x3A\x22h\x22\x,x \x221828\x22,\x22situation\x22\x3A\x22OpenPlay\x22,\x22season\x22\x3A\x222020\x22,\x22shotType\x22\x3A\x22LeftFoot\x22,\x22match_id\x22\x3A\x2214628\x22,\ x22h_team\x22\x3A\x22Manchester\x20United\x22,\x22a_team\x22\x3A\x22Sheffield\x20United\x22,\x22h_goals\x22\x3A\x221\x22,\x22a_goals\x22\x3A\x222\x22,\x22date\ x22\x3A\x222021\x2D01\x2D27\x2020\x3A15\x3A00\x22,\x22player_assisted\x22\x3Anull,\x22lastAction\x22\x3A\x22None\x22\x7D,\x7B\x22id\x22\x3A\x22401358\x ,\x22minute\x22\x3A\x2276\x22,\x22result\x22\x3A\x22MissedShots\x22,\x22X\x22\x3 A\x220.924000015258789\x22,\x22Y\x22\x3A\x220.5070000076293946\x22,\x22xG\x22\x3A\x220.10926949232816696\x22,\x22player\x22h\x3A\x22,x22h\x3A\x22 x22\x3A\x22h\x22,\x22player_id\x22\x3A\x221740\x22,\x22situation\x22\x3A\x22FromCorner\x22,\x22season\x22\x3A\x222020\x22,\x22shotType\x22\x3A\x22Head\ x22,\x22match_id\x22\x3A\x2214628\x22,\x22h_team\x22\x3A\x22Manchester\x20United\x22,\x22a_team\x22\x3A\x22Sheffield\x20United\x22,\x22h_goals\x22\x3A\x221\x22, \x22a_goals\x22\x3A\x222\x22,\x22date\x22\x3A\x222021\x2D01\x2D27\x2020\x3A15\x3A00\x22,\x22player_assisted\x22\x3A\x22Alex\x20Telles\x22,\x22lastAction\x22\ x3A\x22Cross\x22\x7D,\x7B\x22id\x22\x3A\x22401359\x22,\x22minute\x22\x3A\x2281\x22,\x22result\x22\x3A\x22MissedShots\x22,\x22X\x22\x3A\ x220.8630000305175781 \ X22,\ x22Y \ X22 \ X3A \ x220.5570000076293945 \ X22,\ x22xG \ X22 \ X3A \ x220.06367684155702591 \ X22,\ x22player \ X22 \ X3A \ x22Edinson \ x20Cavani \ X22,\ x22h_a \ X22 \ x3A\x22h\x22,\x22player_id\x22\x3A\x223294\x22,\x22情况\x22\x3A\x22打开播放\x22,\x22season\x22\x3A\x222020\x22,\x22shotType\x22\x3A\x22LeftFoot\x22,\x22match_id\x22\x3A\x2214628\x22,\x22h_team\x22\x3A\x22Manchester\x20United\x22, \x22a_team\x22\x3A\x22Sheffield\x20United\x22,\x22h_goals\x22\x3A\x221\x22,\x22a_goals\x22\x3A\x222\x22,\x22date\x22\x3A\x222021\x2D01\x2D27\x2020\ x3A15\x3A00\x22,\x22player_assisted\x22\x3Anull,\x22lastAction\x22\x3A\x22None\x22\x7D,\x7B\x22id\x22\x3A\x22401360\x22,\x22minute\x22\x3A\x2285\x22, \x22result\x22\x3A\x22BlockedShot\x22,\x22X\x22\x3A\x220.86\x22,\x22Y\x22\x3A\x220.775\x22,\x22xG\x22\x3A\x220.02079056017100811\x22, \x22player\x22\x3A\x22Bruno\x20Fernandes\x22,\x22h_a\x22\x3A\x22h\x22,\x22player_id\x22\x3A\x221228\x22,\x22situation\x22\x3A\x22FromCorner\x22,\x22season\x22 \x3A\x222020\x22,\x22shotType\x22\x3A\x22RightFoot\x22,\x22match_id\x22\x3A\x2214628\x22,\x22h_team\x22\x3A\x22Manchester\x20United\x22,\x22a_team\x22\x3A\x22Shef \x20United\x22,\x22h_goals\x22\x3A\x221\x22,\x22a_goals\x22\x3A\x222\x22,\x22date\x22\x3 A\x222021\x2D01\x2D27\x2020\x3A15\x3A00\x22,\x22player_assisted\x22\x3A\x22Luke\x20Shaw\x22,\x22lastAction\x22\x3A\x22Pass\x22\x7D\x5D,\x22a\x22\x3A \x5B\x7B\x22id\x22\x3A\x22401341\x22,\x22minute\x22\x3A\x2215\x22,\x22result\x22\x3A\x22SavedShot\x22,\x22X\x22\x3A\x220.8690000152587891\x22, \x22Y\x22\x3A\x220.5479999923706055\x22,\x22xG\x22\x3A\x220.12690183520317078\x22,\x22player\x22\x3A\x22Billy\x20Sharp\x22,\x\x222a\x22\A,x \x22player_id\x22\x3A\x227712\x22,\x22situation\x22\x3A\x22OpenPlay\x22,\x22season\x22\x3A\x222020\x22,\x22shotType\x22\x3A\x22LeftFoot\x22,\x22match_id\x22\x3A \x2214628\x22,\x22h_team\x22\x3A\x22Manchester\x20United\x22,\x22a_team\x22\x3A\x22Sheffield\x20United\x22,\x22h_goals\x22\x3A\x221\x22,\x22a_goals\x22\x3A\x22 \x22,\x22date\x22\x3A\x222021\x2D01\x2D27\x2020\x3A15\x3A00\x22,\x22player_assisted\x22\x3A\x22John\x20Fleck\x22,\x22lastAction\x22\x3A\x22Throughball\x22\x7D, \x7B\x22id\x22\x3A\x22401343\x22,\x22minute\x22\x3A\x2222\x22,\x22result\x22\x3A\x2 2进球\x22,\x22X\x22\x3A\x220.98\x22,\x22Y\x22\x3A\x220.49400001525878906\x22,\x22xG\x22\x3A\x220.15722613036632538\x22,\x22球员\x x22Kean\x20Bryan\x22,\x22h_a\x22\x3A\x22a\x22,\x22player_id\x22\x3A\x229163\x22,\x22situation\x22\x3A\x22FromCorner\x22,\x22season\x22\x3A\x222020\x22, \x22shotType\x22\x3A\x22Head\x22,\x22match_id\x22\x3A\x2214628\x22,\x22h_team\x22\x3A\x22Manchester\x20United\x22,\x22a_team\x22\x3A\x22Sheffield\x20United\x22,\x22h_goals \x22\x3A\x221\x22,\x22a_goals\x22\x3A\x222\x22,\x22date\x22\x3A\x222021\x2D01\x2D27\x2020\x3A15\x3A00\x22,\x22player_assisted\x22\x3A\x22John\ x20Fleck\x22,\x22lastAction\x22\x3A\x22Cross\x22\x7D,\x7B\x22id\x22\x3A\x22401351\x22,\x22minute\x22\x3A\x2255\x22,\x22result\x22\x3A\x22MissedShots\ x22,\x22X\x22\x3A\x220.8430000305175781\x22,\x22Y\x22\x3A\x220.29600000381469727\x22,\x22xG\x22\x3A\x220.0439084991822706\2\x2xA\x222706\2\x2xA\x x20McGoldrick\x22,\x22h_a\x22\x3A\x22a\x22,\x22player_id\x22\x3A\x227711\x22,\x22situation\x22\ x3A\x22OpenPlay\x22,\x22season\x22\x3A\x222020\x22,\x22shotType\x22\x3A\x22RightFoot\x22,\x22match_id\x22\x3A\x2214628\x22,\x22h_team\x22\x3A\x22Manchester\x20United\ x22,\x22a_team\x22\x3A\x22Sheffield\x20United\x22,\x22h_goals\x22\x3A\x221\x22,\x22a_goals\x22\x3A\x222\x22,\x22date\x22\x3A\x222021\x2D01\x2D27\ x2020\x3A15\x3A00\x22,\x22player_assisted\x22\x3A\x22George\x20Baldock\x22,\x22lastAction\x22\x3A\x22Pass\x22\x7D,\x7B\x22id\x22\x3A\x22401355\x22,\x22分钟\ x22\x3A\x2273\x22,\x22result\x22\x3A\x22BlockedShot\x22,\x22X\x22\x3A\x220.875\x22,\x22Y\x22\x3A\x220.3609999847412109\x22,\x22xG\x22\ x3A\x220.07747054845094681\x22,\x22player\x22\x3A\x22Oliver\x20Burke\x22,\x22h_a\x22\x3A\x22a\x22,\x22player_id\x22\x3A\x225256\x22\x3A2\x x22OpenPlay\x22,\x22season\x22\x3A\x222020\x22,\x22shotType\x22\x3A\x22RightFoot\x22,\x22match_id\x22\x3A\x2214628\x22,\x22h_team\x22\x3A\x22Manchester\x20United\x22, \x22a_team\x22\x3A\x22Sheffield\x20United\x22,\x22h_goals\x22\x3A\x221\x22,\x 22a_goals\x22\x3A\x222\x22,\x22date\x22\x3A\x222021\x2D01\x2D27\x2020\x3A15\x3A00\x22,\x22player_assisted\x22\x3A\x22John\x20Lundstram\x22,\x22lastAction\x22\x \x22Pass\x22\x7D,\x7B\x22id\x22\x3A\x22401356\x22,\x22minute\x22\x3A\x2273\x22,\x22result\x22\x3A\x22Goal\x22,\x22X\x22\x3A\x220 0.8769999694824219 \ X22,\ x22Y \ X22 \ X3A \ x220.3079999923706055 \ X22,\ x22xG \ X22 \ X3A \ x220.28404054045677185 \ X22,\ x22player \ X22 \ X3A \ x22Oliver \ x20Burke \ X22,\ x22h_a \ X22 \ X3A \x22a\x22,\x22player_id\x22\x3A\x225256\x22,\x22situation\x22\x3A\x22OpenPlay\x22,\x22season\x22\x3A\x222020\x22,\x22shotType\x22\x3A\x22RightFoot\x22,\ x22match_id\x22\x3A\x2214628\x22,\x22h_team\x22\x3A\x22Manchester\x20United\x22,\x22a_team\x22\x3A\x22Sheffield\x20United\x22,\x22h_goals\x22\x3A\x221\x22,\x22a_go x22\x3A\x222\x22,\x22date\x22\x3A\x222021\x2D01\x2D27\x2020\x3A15\x3A00\x22,\x22player_assisted\x22\x3A\x22John\x20Lundstram\x22,\x22lastAction\x22\x3A\x22Pass \x22\x7D\x5D\x7D')

谁能帮我调试代码,以便我可以获取 JSON 格式的数据?

【问题讨论】:

  • 你能打印json_data,把它的内容放在你选择的分隔符之间,比如print("**{json_data}**"),看看它是开始还是后面是换行符或空格
  • 请看我已经用完整的镜头字符串更新了这个问题,我正在使用 "('"+1 因为这是我获取数据的起始索引。@JulesG.M.

标签: python json web-scraping beautifulsoup data-analysis


【解决方案1】:

问题在于您的json_data,因为字符串以'{ 开头。你想要的起始索引实际上是{ 前面的一个索引值,所以你想在索引开始处添加 2,而不是 1:

index_start = strings.index("('")+2 而不是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)

输出:

{'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'}]}

你也可以替换:

#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]

与:

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

【讨论】:

    猜你喜欢
    • 2021-09-25
    • 2023-03-25
    • 2020-11-11
    • 2017-05-09
    • 2019-08-13
    • 1970-01-01
    • 2021-10-21
    • 2021-05-21
    相关资源
    最近更新 更多