GrandaddyOG GrandaddyOG - 5 months ago 107
JSON Question

Scraping- Error: No JSON object could be decoded

So i'm trying to scrape the table on this page for educational purposes: https://www.whoscored.com/Regions/252/Tournaments/2/Seasons/5826/Stages/12496/TeamStatistics/England-Premier-League-2015-2016

My print response gives

<Response [200]>


This would mean it's good ,right? But when I put response.json() in a variable and try to print it out I get 'ValueError: No JSON Object could be decoded'

Here is my code :

import requests


headers = {
'accept-encoding': 'gzip, deflate, sdch, br',
'x-requested-with': 'XMLHttpRequest',
'accept-language': 'nl-NL,nl;q=0.8,en-US;q=0.6,en;q=0.4',
'user-agent': 'Mozilla/5.0 (Windows NT 6.2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36',
'accept': 'application/json, text/javascript, */*; q=0.01',
'referer': 'https://www.whoscored.com/Regions/252/Tournaments/2/Seasons/5826/Stages/12496/TeamStatistics/England-Premier-League-2015-2016',
'model-last-mode': 'GgVBlUgZgMvkAZA4DkgCYAYJauGyQ5BQAQW5ZLGd5qk=',
'authority': 'www.whoscored.com',
'cookie': 'visid_incap_774904=E5yi1BIhTqKa7QY7CTHvbCgWQlcAAAAAQUIPAAAAAAAbmGcCJZ/yA5tiB2vsu1g6; crtg_rta=cc970250d%3Bcc300600d%3Bhw970250d%3B; incap_ses_128_774904=GEttC+rfv2wXecdRg7/GAUAaeVcAAAAA5ZSbfyTF02TorJxEKZxkUg==; _gat=1; _ga=GA1.2.1512369618.1463948865',
}

params = {
'category': 'shots',
'subcategory': 'zones',
'statsAccumulationType': '0',
'field': '',
'tournamentOptions': '',
'timeOfTheGameStart': '0',
'timeOfTheGameEnd': '5',
'teamIds': '',
'stageId': '12496',
'sortBy': 'Rating',
'sortAscending': '',
'page': '1',
'numberOfTeamsToPick': '',
'isCurrent': 'true',
'formation': ''
}

response = requests.get('https://www.whoscored.com/StatisticsFeed/1/GetTeamStatistics?category=shots&subcategory=zones&statsAccumulationType=0&field=&tournamentOptions=&timeOfTheGameStart=0&timeOfTheGameEnd=5&teamIds=&stageId=12496&sortBy=Rating&sortAscending=&page=1&numberOfTeamsToPick=&isCurrent=true&formation=', headers=headers, params=params)


data = response.json()
print data

Answer

You're printing an object, you need to call text attribute . Response object don't have a json attribute .

result = requests.get(url)
print result.text

If you want to get a json result, you will need to parse the result which is a string and convert it to a dict, and then use json.dumps to turn it into json

Comments