`request.json()` throwing "Response not interpretable as json" using requests - python

import requests
import json
def get_movies_from_tastedive(movie):
d = {"q":movie,"type":"movie","limit":"5"}
movies = requests.get("https://tastedive.com/api/similar",params=d)
x = movies.json()
print(x)
I am running the above code trying to access TasteDive API but I keep getting this error:
{'error': 'Response not interpretable as json. Try printing the .text attribute'}
Why is this happening?

The endpoint doesn't return JSON.
This probably means that you entered a file name having issues (maybe strange / not accepted characters?).
Please try this code to see what it returns, in my examples it returns always JSON data:
import requests
import json
def get_movies_from_tastedive(movie):
d = {"q":movie,"type":"movie","limit":"5"}
movies = requests.get("https://tastedive.com/api/similar",params=d)
try:
return movies.json()
except Exception as e:
print(e)
return movies.text
print("Getting data for movie Seven:")
print(get_movies_from_tastedive("Seven"))
print("\nGetting data for movie Sevssssen:")
print(get_movies_from_tastedive("Sevssssen"))
Output
Getting data for movie Seven:
{u'Similar': {u'Info': [{u'Type': u'movie', u'Name': u'Seven'}], u'Results': [{u'Type': u'movie', u'Name': u'Primal Fear'}, {u'Type': u'movie', u'Name': u'The Usual Suspects'}, {u'Type': u'movie', u'Name': u'The Game'}, {u'Type': u'movie', u'Name': u'Insomnia'}, {u'Type': u'movie', u'Name': u'American History X'}]}}
Getting data for movie Sevssssen:
{u'Similar': {u'Info': [{u'Type': u'unknown', u'Name': u'sevssssen'}, {u'Type': u'unknown', u'Name': u'sevssssen'}], u'Results': []}}

Your problem looks very similar to the same I got on Data Collection and Processing with Python final assignment.
I fixed it using requests_with_caching instead of requests on Runestone's task
import requests_with_caching
def get_movies_from_tastedive(criteria):
params = {'q': criteria, 'type': 'movies', 'limit': '5'}
return requests_with_caching.get('https://tastedive.com/api/similar', params = params).json()

import requests_with_caching
import json
def get_movies_from_tastedive(name_movie):
parameters = {'q':name_movie, 'type':'movies', 'limit':5}
a = requests_with_caching.get( 'https://tastedive.com/api/similar', params = parameters)
b = a.json()
return b
print(get_movies_from_tastedive("Bridesmaids"))
print(get_movies_from_tastedive("Black Panther"))
----------------------------------------------------------------------------
Output
found in permanent_cache
{'Similar': {'Info': [{'Name': 'Bridesmaids', 'Type': 'movie'}], 'Results': [{'Name': 'Baby Mama', 'Type': 'movie'}, {'Name': 'The Five-Year Engagement', 'Type': 'movie'}, {'Name': 'Bachelorette', 'Type': 'movie'}, {'Name': 'The Heat', 'Type': 'movie'}, {'Name': 'Date Night', 'Type': 'movie'}]}}
found in permanent_cache
{'Similar': {'Info': [{'Name': 'Black Panther', 'Type': 'movie'}], 'Results': [{'Name': 'Captain Marvel', 'Type': 'movie'}, {'Name': 'Avengers: Infinity War', 'Type': 'movie'}, {'Name': 'Ant-Man And The Wasp', 'Type': 'movie'}, {'Name': 'The Fate Of The Furious', 'Type': 'movie'}, {'Name': 'Deadpool 2', 'Type': 'movie'}]}}

I too had the same issue in Coursera's system (even with requests_with_caching) where it worked on my machine. The fix is to change your parameters to reflect proper JSON formatting (i.e. space after each colon and comma):
Your code:
d = {"q":movie,"type":"movie","limit":"5"}
Proper code which fixed my issue (I had already swapped to requests_with_caching before):
d = {"q": movie, "type": "movie", "limit": "5"}
After this change my test began miraculously passing.

Actually you are the calling the API wrongly. first of all you need to use data= or json= depending on type of content you are sending to the API if it's form-data or JSON.
Secondly: movie is invalid value because it's need to be wrapped with "movie" < double quotation.
Below you should be able to call the API correctly :
import requests
import json
data = {
"q": "movie",
"type": "movie",
"limit": "5"
}
def main(url):
r = requests.get(url, json=data).json()
print(r)
#print(r.keys()) # to see the keys as it's now a JSON dict.
dumper = json.dumps(r, indent=4)
# print(dumper) #to see it in nice formatted tree.
main("https://tastedive.com/api/similar")
Output:
{'Similar': {'Info': [{'Name': '!!!', 'Type': 'music'}], 'Results': [{'Name': 'Meeting Of Important People', 'Type': 'music'}, {'Name': 'Soldout', 'Type': 'music'}, {'Name': 'La Tour Montparnasse Infernale', 'Type': 'movie'}, {'Name': 'Young & Sick', 'Type': 'music'}, {'Name': 'The Vanity Project', 'Type': 'music'}, {'Name': 'Tyler Bryant & The Shakedown', 'Type': 'music'}, {'Name': 'Thirsty Fish', 'Type': 'music'}, {'Name': 'Sombear', 'Type': 'music'}, {'Name': 'The High Court', 'Type': 'music'}, {'Name': 'Better Luck Next Time', 'Type': 'music'}, {'Name': 'Stars Of Track And Field',
'Type': 'music'}, {'Name': 'Beachwood Sparks', 'Type': 'music'}, {'Name':
'Tinted Windows', 'Type': 'music'}, {'Name': 'Promise Of Redemption', 'Type': 'music'}, {'Name': 'The Music', 'Type': 'music'}, {'Name': 'Pretty Girls Make Graves', 'Type': 'music'}, {'Name': 'Zach Gill', 'Type': 'music'}, {'Name': 'Chappo', 'Type': 'music'}, {'Name': 'Kisses', 'Type': 'music'}, {'Name': 'Jarle Bernhoft', 'Type': 'music'}]}}
And for the dumper:
{
"Similar": {
"Info": [
{
"Name": "!!!",
"Type": "music"
}
],
"Results": [
{
"Name": "Meeting Of Important People",
"Type": "music"
},
{
"Name": "Soldout",
"Type": "music"
},
{
"Name": "La Tour Montparnasse Infernale",
"Type": "movie"
},
{
"Name": "Young & Sick",
"Type": "music"
},
{
"Name": "The Vanity Project",
"Type": "music"
},
{
"Name": "Tyler Bryant & The Shakedown",
"Type": "music"
},
{
"Name": "Thirsty Fish",
"Type": "music"
},
{
"Name": "Sombear",
"Type": "music"
},
{
"Name": "The High Court",
"Type": "music"
},
{
"Name": "Better Luck Next Time",
"Type": "music"
},
{
"Name": "Stars Of Track And Field",
"Type": "music"
},
{
"Name": "Beachwood Sparks",
"Type": "music"
},
{
"Name": "Tinted Windows",
"Type": "music"
},
{
"Name": "Promise Of Redemption",
"Type": "music"
},
{
"Name": "The Music",
"Type": "music"
},
{
"Name": "Pretty Girls Make Graves",
"Type": "music"
},
{
"Name": "Zach Gill",
"Type": "music"
},
{
"Name": "Chappo",
"Type": "music"
},
{
"Name": "Kisses",
"Type": "music"
},
{
"Name": "Jarle Bernhoft",
"Type": "music"
}
]
}
}

Related

Looping through JSON (Foursquare API)

I am trying to extract data from a JSON file, of which a snippet is below. I want to loop through it to get all categories>name and get , as in this case, "Convenience Store" as a result.
{
'meta': {
'code': 200,
'requestId': '5ea184baedbcad001b7a3f8c'
},
'response': {
'venues': [
{
'id': '4d03b2f6dc45a093b4b0e5c6',
'name': 'Ozbesa Market',
'location': {
'address': 'Acibadem basogretmen sokak',
'lat': 41.00622726261631,
'lng': 29.051791450375678,
'labeledLatLngs': [
{
'label': 'display',
'lat': 41.00622726261631,
'lng': 29.051791450375678
}
],
'distance': 92,
'cc': 'TR',
'country': 'Türkiye',
'formattedAddress': [
'Acibadem basogretmen sokak',
'Türkiye'
]
},
'categories': [
{
'id': '4d954b0ea243a5684a65b473',
'name': 'Convenience Store',
'pluralName': 'Convenience Stores',
'shortName': 'Convenience Store',
'icon': {
'prefix': 'https://ss3.4sqi.net/img/categories_v2/shops/conveniencestore_',
'suffix': '.png'
},
'primary': True
}
],
'referralId': 'v-1587643627',
'hasPerk': False
},
Here is my for loop, please help me fix it. It is only returning just convenience stores, but there are also others like 'shopping mall', 'residential building', etc.
for ven in json_data:
for cat in ven:
print(json_data['response']['venues'][0]['categories'][0]['name'])
Thanks in advance!
For the sake of example, I elided some of the per-venue data and added some categories... but as I mentioned in the comment, you're not using the values you loop over.
json_data = {
"meta": {"code": 200, "requestId": "5ea184baedbcad001b7a3f8c"},
"response": {
"venues": [
{
"name": "Ozbesa Market",
"categories": [
{"name": "Convenience Store", "primary": True},
{"name": "Imaginary Category", "primary": False},
],
},
{
"name": "Another Location",
"categories": [
{"name": "Bus Station", "primary": True},
{"name": "Fun Fair", "primary": False},
],
},
]
},
}
for venue in json_data["response"]["venues"]:
print(venue["name"])
for cat in venue["categories"]:
print("..", cat["name"])
will output e.g.
Ozbesa Market
.. Convenience Store
.. Imaginary Category
Another Location
.. Bus Station
.. Fun Fair

Not able to Parse JSON Response

I am using Python2.7 and simple json module, I am able to get a response but when i want to do something with this JSON Response I am not able to do.
Python Code :
query_url = self.api_url + 'projects'
try:
req = urllib2.Request(query_url, None, {"Authorization": self._auth})
handler = self._opener.open(req)
except urllib2.HTTPError, e:
print e.headers
raise e
print simplejson.load(handler)
JSON Response:
{'start': 0, 'nextPageStart': 166, 'values': [{'description': 'This Repo is created to maintain the code versioning accordingly for My project', 'links': {'self': [{'href': 'https://bitbucket.xyz.xyz/projects/My'}]}, 'id': 757, 'key': 'MY', 'type': 'NORMAL', 'public': False, 'name': 'hub'},{'description': 'Services ', 'links': {'self': [{'href': 'https://bitbucket.xyz.xyz/projects/Hello'}]}, 'id': 1457, 'key': 'HE', 'type': 'NORMAL', 'public': False, 'name': 'Hello'}], 'limit': 25, 'isLastPage': False, 'size': 25}
Few data i have removed just kept first and last.
The error which i am observing
Error: Parse error on line 1:
..."NORMAL", "public": False, "name": "Advi
-----------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[', got 'undefined'
Can some body help me here what i am doing wrong here.
Because your json is invalid. Here it is valid version of your Json data.
First error Strings should be wrapped in double quotes.
Secondly boolean variable in javascprit is lower case. Use false instead of False
{
"start": 0,
"nextPageStart": 166,
"values": [{
"description": "This Repo is created to maintain the code versioning accordingly for My project",
"links": {
"self": [{
"href": "https://bitbucket.xyz.xyz/projects/My"
}]
},
"id": 757,
"key": "MY",
"type": "NORMAL",
"public": false,
"name": "hub"
},
{
"description": "Services ",
"links": {
"self": [{
"href": "https://bitbucket.xyz.xyz/projects/Hello"
}]
},
"id": 1457,
"key": "HE",
"type": "NORMAL",
"public": false,
"name": "Hello"
}
],
"limit": 25,
"isLastPage": false,
"size": 25
}

Json format for python

I'm rewriting a view based on what I know the final output should be in json but it's returning the dictionary as a string.
new output
{
"results":
["
{
'plot': u'',
'runtime': u'N/A',
'description': u'x',
'videos': [
{
'id': 823,
'name': u'x',
'youtube_id': u'FtcubOnXgZk'
}
],
'country': u'India',
'writer': u'Neetu Varma, Ranjeev Verma',
'name': u'Chalk N Duster',
'id': 940,
'director': u'Jayant Gilatar',
'hot': True,
'content': u'x',
'actors': u'Shabana Azmi, Arya Babbar, Gavie Chahal, Juhi Chawla',
'year': 2015,
'images': [
{'small': '/media/cache/62/fd/62fd5158d281c042e3cf1f919183e94e.jpg', 'medium': '/media/cache/5e/32/5e32ebb1a4d25bba0d0c70b4b448e948.jpg'}],
'trailer_youtube_id': u'FtcubOnXgZk',
'type': 'movie',
'slug': u'chalk-n-duster',
'categories': [{'parent_id': 2, 'id': 226, 'name': u'Drama'}],
'shows': {
'starts': '2016-01-16',
'booking_url': u'',
'venue': {
'address': u'',
'id': 854,
'name': u'Nyali Cinemax',
'area': {
'id': 52,
'parent': {
'id': 48,
'name': u'Mombasa'
},
'name': u'Nyali'
}
},
'starts_time': '18:30:00'
}
}", "{'plot': u'' ....
old output
"results": [
{
"actors": "x",
"categories": [
{
"id": 299,
"name": "Biography",
"parent_id": 2
},
],
"content": "x",
"country": "x",
"description": "x",
"director": "x",
"hot": true,
"id": 912,
"images": [
{
"medium": "/media/cache/d2/b3/d2b3a7885e7c39bfc5c2b297b66619c5.jpg",
"small": "/media/cache/e2/d0/e2d01b2c7c77d3590536666de4a7fd7d.jpg"
}
],
"name": "Bridge of Spies",
"plot": "x",
"runtime": "141 min",
"shows": [
{
"booking_url": "",
"starts": "2015-11-27",
"starts_time": "16:30:00",
"venue": {
"address": "The Junction Shopping Mall",
"area": {
"id": 68,
"name": "Ngong Road",
"parent": {
"id": 2,
"name": "Nairobi"
}
},
"id": 1631,
"name": "Century Cinemax Junction"
}
},
],
"slug": "bridge-of-spies",
"trailer_youtube_id": "",
"type": "movie",
"videos": [
{
"id": "795",
"name": "Bridge of Spies",
"youtube_id": "2-2x3r1m2I4"
}
],
"writer": "Matt Charman, Ethan Coen, Joel Coen",
"year": 2015
}, ...
]
Here's the view, I know the shows should also be a list, but in order to start testing I'll need the data to come in the right format. If it's involves too much rewriting I'm okay with links and explanation.
#memoize(timeout=60*60)
def movies_json():
today = datetime.date.today()
movies = Movie.objects.filter(shows__starts__gte=today)
results = []
number = len(movies)
for movie in movies:
print "Now Remaining: {0}".format(number)
number -= 1
medium = get_thumbnail(movie.picture(), '185x274', crop='center', quality=99).url
small = get_thumbnail(movie.picture(), '50x74', crop='center', quality=99).url
movie_details = {
'director':movie.director,
'plot':movie.plot,
'actors':movie.actors,
'content':movie.content,
'country':movie.country,
'description':movie.description,
'hot':movie.hot,
'id':movie.id,
'images':[{'medium':medium, 'small':small}],
'name':movie.name,
'plot':movie.plot,
'runtime':movie.runtime,
'slug':movie.slug,
'type':'movie',
'writer':movie.writer,
'year':movie.year,
}
youtube_details = movie.videos.filter(youtube_id__isnull=False)[0]
movie_details['trailer_youtube_id'] = youtube_details.youtube_id if youtube_details.youtube_id else ""
movie_details['videos'] = [
{
'id':youtube_details.id,
'name':movie.name,
'youtube_id':youtube_details.youtube_id,
}
]
shows = []
for show in movie.shows.all():
show_details = {
'booking_url':show.booking_url,
'starts':show.starts.isoformat(),
'starts_time':show.starts_time.isoformat(),
'venue': {
'address':show.venue.address,
'area': {
'id': show.venue.area.id,
'name': show.venue.area.name,
'parent': {
'id': show.venue.area.parent.id,
'name': show.venue.area.parent.name,
}
},
'id': show.venue.id,
'name': show.venue.name,
}
}
shows.append(show_details)
movie_details['shows'] = show_details
category_list = []
for category in movie.categories.all():
category_details = {
'id':category.id,
'name':category.name,
'parent_id':category.parent.id,
}
category_list.append(category_details)
movie_details['categories'] = category_list
results.append(movie_details)
return results
The data is returned by django rest framework 0.4.0
import json
json_obj = json.load(json_string)

Scrapy data selection

I would like to scrap data from this plain text :
"data": [
{
"id": "10150635906994798_21377910",
"from": {
"id": "100001249878256",
"location" : "Stockholm"
"name": "Mouhamadoul Moussa"
},
"message": "#Yeaaaahh!!! \u2665",
},
{
"id": "10150635906994798_21392047",
"from": {
"id": "100000648164454",
"location" : "Malmo"
"name": "mallow ty"
},
"message": "droit au butttttttttttttttttt",
},
]
but I would like to retrieve only second id, xpath for id selection
response.selector.xpath ('//*[contains(text(), "id")]')
Output should be :
100000648164454
100001249878256
That's not a plain text ! that's a json. However, you can store it as a dictionary:
>>> a = {'data': [{'from': {'id': '100001249878256',
... 'location': 'Stockholm',
... 'name': 'Mouhamadoul Moussa'},
... 'id': '10150635906994798_21377910',
... 'message': '#Yeaaaahh!!! \\u2665'},
... {'from': {'id': '100000648164454', 'location': 'Malmo', 'name': 'mallow ty'},
... 'id': '10150635906994798_21392047',
... 'message': 'droit au butttttttttttttttttt'}]}
>>> for data in a['data']:
... print data['from']['id']
...
100001249878256
100000648164454

Work with JSON file on Python

I have a Google Chrome Bookmark file, and it's in JSON format
{
"checksum": "b884cbfb1a6697fa9b9eea9cb2054183",
"roots": {
"bookmark_bar": {
"children": [ {
"date_added": "12989159740428363",
"id": "4",
"name": "test2",
"type": "url",
"url": "chrome://bookmarks/#1"
} ],
"date_added": "12989159700896551",
"date_modified": "12989159740428363",
"id": "1",
"name": "bookmark_bar",
"type": "folder"
},
"other": {
"children": [ {
"date_added": "12989159740428363",
"id": "4",
"name": "test",
"type": "url",
"url": "chrome://bookmarks/#1"
} ],
"date_added": "12989159700896557",
"date_modified": "0",
"id": "2",
"name": "aaa",
"type": "folder"
},
"synced": {
"children": [ ],
"date_added": "12989159700896558",
"date_modified": "0",
"id": "3",
"name": "bbb",
"type": "folder"
}
},
"version": 1
}
and in Python format:
{'checksum': 'b884cbfb1a6697fa9b9eea9cb2054183', 'version': 1, 'roots': {'synced': {'name': 'bbb', 'date_modified': '0', 'children': [], 'date_added': '12989159700896558', 'type': 'folder', 'id': '3'}, 'bookmark_bar': {'name': 'bookmark_bar', 'date_modified': '12989159740428363', 'children': [{'url': 'chrome://bookmarks/#1', 'date_added': '12989159740428363', 'type': 'url', 'id': '4', 'name': 'test2'}], 'date_added': '12989159700896551', 'type': 'folder', 'id': '1'}, 'other': {'name': 'aaa', 'date_modified': '0', 'children': [{'url': 'chrome://bookmarks/#1', 'date_added': '12989159740428363', 'type': 'url', 'id': '4', 'name': 'test'}], 'date_added': '12989159700896557', 'type': 'folder', 'id': '2'}}}
I'm writing a bookmark manager now.
I want to move the web pages by name.
For example: mv /bookmark_bar/test2 /other/test2
But every web pages are dictionaries, and they are in a list. So, I must use index to locate the web pages, I can't locate them by name.
Any ideas?
Is it what you need https://gist.github.com/3332055 ?
An example of how to iterate over the structure - exactly what you want to do with it then, is up to you:
for root, val in bm['roots'].iteritems():
print root, 'is named', val['name']
for child in val['children']:
print '\t', child['name'], 'is at', child['url']
# -*- coding: utf-8 -*-
import json
def hook(pairs):
o = {}
for k, v in pairs.iteritems():
o[str(k)] = v
return o
jsonString = """{"a":"a","b":"b","c":{"c1":"c1","c2":"c2"}}"""
r = json.loads(jsonString, object_hook=hook)
assert r['c']['c1'] == "c1"
del r['c']['c1']
assert not r['c'].has_key('c1')

Categories