Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 获取JSON文件时语法出错_Python_Django_Api - Fatal编程技术网

Python 获取JSON文件时语法出错

Python 获取JSON文件时语法出错,python,django,api,Python,Django,Api,我在构建Twitter随机引用生成器API时遇到问题。我将遵循本教程: 但我得到了一个他没有的错误。代码如下: import requests api_key = '*****' api_url = 'https://andruxnet-random-famous-quotes.p.rapidapi.com' headers = {'afd9cbe77emshf06f5cb2f889689p1ca1c3jsne6e79ad808cc' : api_key, 'http://andru

我在构建Twitter随机引用生成器API时遇到问题。我将遵循本教程:

但我得到了一个他没有的错误。代码如下:

import requests 

api_key = '*****' 
api_url = 'https://andruxnet-random-famous-quotes.p.rapidapi.com'

headers = {'afd9cbe77emshf06f5cb2f889689p1ca1c3jsne6e79ad808cc' : 
api_key, 'http://andruxnet-random-famous-quotes.p.rapidapi.com' : 
api_url}

# The get method is called when we 
# want to GET json data from an API endpoint
quotes = requests.get(quotes = requests.get(api_url, 
headers=headers)

print(quotes.json())
这就是错误:

File "twitter_bot.py", line 12
print(quotes.json())

SyntaxError: invalid syntax
我做错了什么??(我故意把***放在钥匙上,我知道正确的钥匙应该放在那里)


谢谢大家!

您有一个复制粘贴错误;不知怎的,你已经把
quotes=requests.get(
放了两次

应该是:

# The get method is called when we 
# want to GET json data from an API endpoint
quotes = requests.get(api_url, headers=headers)

print(quotes.json())

教程不太旧,但似乎已经过时了

使用来自我创建的Python代码的示例,该代码提供了来自服务器的一些信息(但仍然没有引用)

结果:

{"message":"You are not subscribed to this API."}
{"message":"You are not subscribed to this API."}
同样适用于
POST

import requests 

url = "https://andruxnet-random-famous-quotes.p.rapidapi.com/?count=10&cat=famous"

headers={
    "X-RapidAPI-Host": "andruxnet-random-famous-quotes.p.rapidapi.com",
    "X-RapidAPI-Key": "afd9cbe77emshf06f5cb2f889689p1ca1c3jsne6e79ad808cc",
    "Content-Type": "application/x-www-form-urlencoded"
}

quotes = requests.post(url, headers=headers)

print(quotes.text)
#print(quotes.json())
结果:

{"message":"You are not subscribed to this API."}
{"message":"You are not subscribed to this API."}


获取引号仍然需要一些工作。

什么语法错误?在哪里?您在这里对
请求做什么。获取(quotes=…)
,看起来您没有指定端点?抱歉@DanielRoseman刚刚更新了它您忘记了
headers=headers)
后面的右括号,但话说回来,你所做的仍然很奇怪。如果你得到了一个SyntaxError,而你没有在实际行中发现它,那么检查它之前的行。还有,数一数上面一行的右括号。太棒了!非常感谢你!为了做更多的事情,我必须订阅这个API,但它要求信用卡。