你好,我目前正在制作一个语音助手,我已经导入了新闻api,我想限制它说的新闻的数量,就像我只想让它阅读3-4条新闻一样,下面是代码:
elif 'news' in query:
try:
jsonObj = urlopen('https://newsapi.org/v2/top-headlines?country=in&apiKey={api-key}')
data = json.load(jsonObj)
i = 1
speak('here are some top news from the times of india')
print('''=============== TIMES OF INDIA ============'''+ '\n')
for item in data['articles']:
print(str(i) + '. ' + item['title'] + '\n')
print(item['description'] + '\n')
speak(str(i) + '. ' + item['title'] + '\n')
i += 1
except Exception as e:
print(str(e))发布于 2021-05-17 02:58:34
简单地将列表切片添加到数据列表中
for item in data['articles'][0:3]:
现在,它将只读前3篇文章
https://stackoverflow.com/questions/67560399
复制相似问题