【发布时间】:2019-01-30 06:08:19
【问题描述】:
我必须在这里遗漏一些东西,因为计算机不会开玩笑,但是这个简单的 for 循环似乎没有给我想要的输出。下面是代码,它使用aztro's API 来获取 12 个生肖中每个星座的今天星座并将它们全部放在一个列表中。
import requests
import json
zodiacSigns = ['Aries', 'Taurus', 'Gemini', 'Cancer', 'Leo', 'Virgo', 'Libra', 'Scorpio', 'Sagittarius', 'Capricorn', 'Aquarius', 'Pisces']
for zodiacSign in zodiacSigns:
params = (('sign','{}'.format(zodiacSign)), ('day','today'))
output = json.loads(requests.post('https://aztro.sameerkumar.website/', params=params).text)
descriptions = []
descriptions.append(output['description'])
print(descriptions)
此代码仅输出双鱼座的星座,即上面列表中的最后一个元素:
["You need to take work more seriously today -- it may be that you've got an opportunity coming up that shouldn't be missed. It's easier than usual for you to make career moves, so go for it!"]
作为参考,此 aztro 的单个生肖 API 的示例输出是:
{
"compatibility":" Virgo",
"date_range":"Jan 20 - Feb 18",
"current_date":"August 23, 2018",
"description":"Today requires a willingness to go deeper than usual -- maybe to explore the nuances of your primary relationship, maybe to really get to know that one client or maybe just reading between the lines.",
"lucky_time":" 10am",
"lucky_number":" 13",
"color":" Navy Blue",
"mood":" Thoughtful"
}
所需的输出将是所有 12 个生肖的星座列表。我似乎无法在这里发现这个问题,所以我会感谢更有经验的眼睛的意见。谢谢!
【问题讨论】:
-
您在每次迭代中将描述定义为一个空列表。将该定义移到循环之前。
-
另外,你不需要
json.loads(),因为requests响应对象有一个.json()函数。
标签: python api for-loop python-requests append