我在构建Twitter随机引号生成器API时遇到问题。我正在关注本教程:
https://www.twilio.com/blog/build-deploy-twitter-bots-python-tweepy-pythonanywhere
但是我得到了他没有的错误。这是代码:
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
我在做什么错? (我故意把***放在钥匙上,我知道应该把正确的钥匙放在那儿)
谢谢!
答案 0 :(得分:0)
您有复制粘贴错误;您以某种方式将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())
答案 1 :(得分:0)
教程不是很旧,但是似乎已经过时了。
使用RapidAPI documentation (for Random Famous Quotes API)中的示例,我创建了Python的代码,该代码提供了一些来自服务器的信息(但仍然没有引号)
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",
}
quotes = requests.get(url, headers=headers)
print(quotes.text)
#print(quotes.json())
结果:
{"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."}
仍然需要一些工作来获取报价。