`request.json()`throwing“响应不能解释为json”使用请求

2024-09-25 06:31:59 发布

您现在位置: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)

我正在运行上面的代码,试图访问TasteDive API,但不断出现以下错误:

{'error': 'Response not interpretable as json. Try printing the .text attribute'}

为什么会这样


Tags: fromhttpsimportcomapijsongetdef
3条回答

你的问题看起来和我在Data Collection and Processing with Python期末作业中遇到的问题非常相似

我在Runestone的任务中使用了requests_with_caching而不是requests修复了它

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

enter image description here

端点不返回JSON。 这可能意味着您输入的文件名有问题(可能是奇怪/不可接受的字符?)

请尝试此代码以查看它返回的内容,在我的示例中,它始终返回JSON数据:

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

输出

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': []}}
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'}]}}

相关问题 更多 >