这个JSON字符串有什么问题?

时间:2015-09-08 22:11:32

标签: python json python-2.7 parsing web-scraping

这是我第一次使用JSON而且我在这里挂了。

我想在Python中使用tackles解析名为json.loads(tackles)的JSON字符串,并收到错误TypeError: expected string or buffer

在此之后我使用了json.loads(str(tackles)),但失败了以下错误ValueError: Expecting property name: line 1 column 2 (char 1)

那么究竟需要改变什么才能使其有效? 最棒的是一个示例,说明如何打印团队名称旁边的所有tackleWontotal值。

这是JSON字符串:

"{
    u'statColumns': [
        u'apps',
        u'subOn',
        u'minsPlayed',
        u'tackleWonTotal',
        u'challengeLost',
        u'tackleTotalAttempted'
    ],
    u'paging': {
        u'firstRecordIndex': 0,
        u'resultsPerPage': 0,
        u'lastRecordIndex': 0,
        u'totalPages': 0,
        u'currentPage': 1,
        u'totalResults': 0
    },
    u'teamTableStats': [ {
        u'seasonId': 0,
        u'tournamentRegionId': 252,
        u'challengeLost': 10.75,
        u'minsPlayed': 360,
        u'name': u'ManCity',
        u'ranking': 1,
        u'rating': 7.297500000000002,
        u'apps': 4,
        u'tackleWonTotal': 14.75,
        u'tournamentName': u'PremierLeague',
        u'tournamentRegionCode': None,
        u'teamName': u'ManchesterCity',
        u'seasonName': None,
        u'regionCode': u'gb-eng',
        u'teamId': 167,
        u'tournamentId': 2,
        u'tackleTotalAttempted': 25.5
    },
    {
        u'seasonId': 0,
        u'tournamentRegionId': 252,
        u'challengeLost': 8.5,
        u'minsPlayed': 360,
        u'name': u'Arsenal',
        u'ranking': 2,
        u'rating': 7.039793956043958,
        u'apps': 4,
        u'tackleWonTotal': 20.75,
        u'tournamentName': u'PremierLeague',
        u'tournamentRegionCode': None,
        u'teamName': u'Arsenal',
        u'seasonName': None,
        u'regionCode': u'gb-eng',
        u'teamId': 13,
        u'tournamentId': 2,
        u'tackleTotalAttempted': 29.25
    },
    {
        u'seasonId': 0,
        u'tournamentRegionId': 252,
        u'challengeLost': 10.25,
        u'minsPlayed': 360,
        u'name': u'Swansea',
        u'ranking': 3,
        u'rating': 6.999136904761905,
        u'apps': 4,
        u'tackleWonTotal': 16.25,
        u'tournamentName': u'PremierLeague',
        u'tournamentRegionCode': None,
        u'teamName': u'Swansea',
        u'seasonName': None,
        u'regionCode': u'gb-wls',
        u'teamId': 259,
        u'tournamentId': 2,
        u'tackleTotalAttempted': 26.5
    },
    {
        u'seasonId': 0,
        u'tournamentRegionId': 252,
        u'challengeLost': 7.75,
        u'minsPlayed': 360,
        u'name': u'Southampton',
        u'ranking': 4,
        u'rating': 6.945068681318677,
        u'apps': 4,
        u'tackleWonTotal': 16.75,
        u'tournamentName': u'PremierLeague',
        u'tournamentRegionCode': None,
        u'teamName': u'Southampton',
        u'seasonName': None,
        u'regionCode': u'gb-eng',
        u'teamId': 18,
        u'tournamentId': 2,
        u'tackleTotalAttempted': 24.5
    },
    {
        u'seasonId': 0,
        u'tournamentRegionId': 252,
        u'challengeLost': 7.75,
        u'minsPlayed': 360,
        u'name': u'Leicester',
        u'ranking': 5,
        u'rating': 6.922499999999998,
        u'apps': 4,
        u'tackleWonTotal': 23.25,
        u'tournamentName': u'PremierLeague',
        u'tournamentRegionCode': None,
        u'teamName': u'Leicester',
        u'seasonName': None,
        u'regionCode': u'gb-eng',
        u'teamId': 14,
        u'tournamentId': 2,
        u'tackleTotalAttempted': 31.0
    },
    {
        u'seasonId': 0,
        u'tournamentRegionId': 252,
        u'challengeLost': 4.75,
        u'minsPlayed': 360,
        u'name': u'AstonVilla',
        u'ranking': 6,
        u'rating': 6.909995421245423,
        u'apps': 4,
        u'tackleWonTotal': 19.75,
        u'tournamentName': u'PremierLeague',
        u'tournamentRegionCode': None,
        u'teamName': u'AstonVilla',
        u'seasonName': None,
        u'regionCode': u'gb-eng',
        u'teamId': 24,
        u'tournamentId': 2,
        u'tackleTotalAttempted': 24.5
    },
    {
        u'seasonId': 0,
        u'tournamentRegionId': 252,
        u'challengeLost': 8.0,
        u'minsPlayed': 360,
        u'name': u'CrystalPalace',
        u'ranking': 7,
        u'rating': 6.89214285714286,
        u'apps': 4,
        u'tackleWonTotal': 16.0,
        u'tournamentName': u'PremierLeague',
        u'tournamentRegionCode': None,
        u'teamName': u'CrystalPalace',
        u'seasonName': None,
        u'regionCode': u'gb-eng',
        u'teamId': 162,
        u'tournamentId': 2,
        u'tackleTotalAttempted': 24.0
    },
    {
        u'seasonId': 0,
        u'tournamentRegionId': 252,
        u'challengeLost': 7.75,
        u'minsPlayed': 360,
        u'name': u'Everton',
        u'ranking': 8,
        u'rating': 6.876071428571427,
        u'apps': 4,
        u'tackleWonTotal': 16.5,
        u'tournamentName': u'PremierLeague',
        u'tournamentRegionCode': None,
        u'teamName': u'Everton',
        u'seasonName': None,
        u'regionCode': u'gb-eng',
        u'teamId': 31,
        u'tournamentId': 2,
        u'tackleTotalAttempted': 24.25
    },
    {
        u'seasonId': 0,
        u'tournamentRegionId': 252,
        u'challengeLost': 10.5,
        u'minsPlayed': 360,
        u'name': u'WestHam',
        u'ranking': 9,
        u'rating': 6.86017857142857,
        u'apps': 4,
        u'tackleWonTotal': 20.0,
        u'tournamentName': u'PremierLeague',
        u'tournamentRegionCode': None,
        u'teamName': u'WestHam',
        u'seasonName': None,
        u'regionCode': u'gb-eng',
        u'teamId': 29,
        u'tournamentId': 2,
        u'tackleTotalAttempted': 30.5
    },
    {
        u'seasonId': 0,
        u'tournamentRegionId': 252,
        u'challengeLost': 8.75,
        u'minsPlayed': 360,
        u'name': u'ManUtd',
        u'ranking': 10,
        u'rating': 6.855535714285715,
        u'apps': 4,
        u'tackleWonTotal': 19.5,
        u'tournamentName': u'PremierLeague',
        u'tournamentRegionCode': None,
        u'teamName': u'ManchesterUnited',
        u'seasonName': None,
        u'regionCode': u'gb-eng',
        u'teamId': 32,
        u'tournamentId': 2,
        u'tackleTotalAttempted': 28.25
    },
    {
        u'seasonId': 0,
        u'tournamentRegionId': 252,
        u'challengeLost': 7.0,
        u'minsPlayed': 360,
        u'name': u'Watford',
        u'ranking': 11,
        u'rating': 6.850975274725273,
        u'apps': 4,
        u'tackleWonTotal': 21.5,
        u'tournamentName': u'PremierLeague',
        u'tournamentRegionCode': None,
        u'teamName': u'Watford',
        u'seasonName': None,
        u'regionCode': u'gb-eng',
        u'teamId': 27,
        u'tournamentId': 2,
        u'tackleTotalAttempted': 28.5
    },
    {
        u'seasonId': 0,
        u'tournamentRegionId': 252,
        u'challengeLost': 10.0,
        u'minsPlayed': 360,
        u'name': u'Liverpool',
        u'ranking': 12,
        u'rating': 6.85006868131868,
        u'apps': 4,
        u'tackleWonTotal': 20.25,
        u'tournamentName': u'PremierLeague',
        u'tournamentRegionCode': None,
        u'teamName': u'Liverpool',
        u'seasonName': None,
        u'regionCode': u'gb-eng',
        u'teamId': 26,
        u'tournamentId': 2,
        u'tackleTotalAttempted': 30.25
    },
    {
        u'seasonId': 0,
        u'tournamentRegionId': 252,
        u'challengeLost': 7.5,
        u'minsPlayed': 360,
        u'name': u'Tottenham',
        u'ranking': 13,
        u'rating': 6.777403846153845,
        u'apps': 4,
        u'tackleWonTotal': 19.5,
        u'tournamentName': u'PremierLeague',
        u'tournamentRegionCode': None,
        u'teamName': u'Tottenham',
        u'seasonName': None,
        u'regionCode': u'gb-eng',
        u'teamId': 30,
        u'tournamentId': 2,
        u'tackleTotalAttempted': 27.0
    },
    {
        u'seasonId': 0,
        u'tournamentRegionId': 252,
        u'challengeLost': 9.25,
        u'minsPlayed': 360,
        u'name': u'WestBrom',
        u'ranking': 14,
        u'rating': 6.69517857142857,
        u'apps': 4,
        u'tackleWonTotal': 18.25,
        u'tournamentName': u'PremierLeague',
        u'tournamentRegionCode': None,
        u'teamName': u'WestBromwichAlbion',
        u'seasonName': None,
        u'regionCode': u'gb-eng',
        u'teamId': 175,
        u'tournamentId': 2,
        u'tackleTotalAttempted': 27.5
    },
    {
        u'seasonId': 0,
        u'tournamentRegionId': 252,
        u'challengeLost': 9.25,
        u'minsPlayed': 360,
        u'name': u'Chelsea',
        u'ranking': 15,
        u'rating': 6.659107142857143,
        u'apps': 4,
        u'tackleWonTotal': 15.25,
        u'tournamentName': u'PremierLeague',
        u'tournamentRegionCode': None,
        u'teamName': u'Chelsea',
        u'seasonName': None,
        u'regionCode': u'gb-eng',
        u'teamId': 15,
        u'tournamentId': 2,
        u'tackleTotalAttempted': 24.5
    },
    {
        u'seasonId': 0,
        u'tournamentRegionId': 252,
        u'challengeLost': 11.5,
        u'minsPlayed': 360,
        u'name': u'Sunderland',
        u'ranking': 16,
        u'rating': 6.656263736263735,
        u'apps': 4,
        u'tackleWonTotal': 26.75,
        u'tournamentName': u'PremierLeague',
        u'tournamentRegionCode': None,
        u'teamName': u'Sunderland',
        u'seasonName': None,
        u'regionCode': u'gb-eng',
        u'teamId': 16,
        u'tournamentId': 2,
        u'tackleTotalAttempted': 38.25
    },
    {
        u'seasonId': 0,
        u'tournamentRegionId': 252,
        u'challengeLost': 10.25,
        u'minsPlayed': 360,
        u'name': u'Bournemouth',
        u'ranking': 17,
        u'rating': 6.64839285714286,
        u'apps': 4,
        u'tackleWonTotal': 17.25,
        u'tournamentName': u'PremierLeague',
        u'tournamentRegionCode': None,
        u'teamName': u'Bournemouth',
        u'seasonName': None,
        u'regionCode': u'gb-eng',
        u'teamId': 183,
        u'tournamentId': 2,
        u'tackleTotalAttempted': 27.5
    },
    {
        u'seasonId': 0,
        u'tournamentRegionId': 252,
        u'challengeLost': 8.5,
        u'minsPlayed': 360,
        u'name': u'Stoke',
        u'ranking': 18,
        u'rating': 6.600714285714285,
        u'apps': 4,
        u'tackleWonTotal': 19.25,
        u'tournamentName': u'PremierLeague',
        u'tournamentRegionCode': None,
        u'teamName': u'Stoke',
        u'seasonName': None,
        u'regionCode': u'gb-eng',
        u'teamId': 96,
        u'tournamentId': 2,
        u'tackleTotalAttempted': 27.75
    },
    {
        u'seasonId': 0,
        u'tournamentRegionId': 252,
        u'challengeLost': 7.75,
        u'minsPlayed': 360,
        u'name': u'Norwich',
        u'ranking': 19,
        u'rating': 6.59885989010989,
        u'apps': 4,
        u'tackleWonTotal': 17.75,
        u'tournamentName': u'PremierLeague',
        u'tournamentRegionCode': None,
        u'teamName': u'Norwich',
        u'seasonName': None,
        u'regionCode': u'gb-eng',
        u'teamId': 168,
        u'tournamentId': 2,
        u'tackleTotalAttempted': 25.5
    },
    {
        u'seasonId': 0,
        u'tournamentRegionId': 252,
        u'challengeLost': 8.25,
        u'minsPlayed': 360,
        u'name': u'Newcastle',
        u'ranking': 20,
        u'rating': 6.596428571428575,
        u'apps': 4,
        u'tackleWonTotal': 18.25,
        u'tournamentName': u'PremierLeague',
        u'tournamentRegionCode': None,
        u'teamName': u'NewcastleUnited',
        u'seasonName': None,
        u'regionCode': u'gb-eng',
        u'teamId': 23,
        u'tournamentId': 2,
        u'tackleTotalAttempted': 26.5
    } ] }"

这是我的完整代码:

import json
import requests

url = "http://www.whoscored.com/StatisticsFeed/1/GetTeamStatistics"

headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36',
           'X-Requested-With': 'XMLHttpRequest',
           'Host': 'www.whoscored.com',
           'Referer': 'http://www.whoscored.com/'}

def GetResponse(category, subcategory):
    params = {
            'category': category,
            'subcategory': subcategory,
            'statsAccumulationType': '0',
            'field': 'Overall',
            'tournamentOptions': '',
            'timeOfTheGameStart': '0',
            'timeOfTheGameEnd': '5',
            'stageId': '12496',
            'sortBy': 'Rating',
            'sortAscending': 'false',
            'page': '1',
            'includeZeroValues': '',
            'numberOfTeamsToPick': '', 
            'isCurrent': 'true',
            'formation': ''
            }  
    return requests.get(url, params=params, headers=headers).json()

tackles = GetResponse('tackles','success');

parsedtackles = json.loads(str(tackles))
print tackles

现在我能够解决这个问题。感谢所有回复的人,您的回复对我来说是一个很大的帮助。

3 个答案:

答案 0 :(得分:0)

事实是,这不是一个合适的JSON字符串。 u'用于Python unicode字符串对象,可能在某处,您的原始JSON被转换为Python对象,然后进行字符串化。

检查更改的位置,然后将其消除,以便为load()方法

提供正确的JSON

答案 1 :(得分:0)

正如其他人所说,这不是JSON;除其他事项外,JSON需要将其键引用为双引号(不是单引号),并禁止使用Python u前缀。

如果这实际上是您获得的数据,那么您最好使用ast.literal_eval。 (Docs here.

那就是说,当你说你试图运行json.loads(str(tackles))时,我会再往前看。

str(tackles)不会产生JSON; json.dumps(tackles)。如果您只想测试JSON生成/解析,请尝试json.loads(json.dumps(tackles))

答案 2 :(得分:0)

或只是

parsedtackles = str (json.dumps(tackles))