如何通过python-json响应对象循环



我有一个股票市场消息的api调用,应该会返回过去7天内的消息。我试图打印所有的数据,但当我运行程序时,它什么都不显示。只是空白。如何循环遍历所有值并打印对象中的所有数据?以下是回应。我需要打印日期、文本、情绪和股票行情。感谢的帮助

{
"data": [
{
"news_url": "",
"image_url": "",
"title": "Attractively-Priced Apple Has Phenomenal Dividend Growth Potential",
"text": "Apple offers investors tremendous capital appreciation and dividend growth potential, in our view. The company is a true financial powerhouse. Initial reports indicate Apple's new iPhone 12 offering, its first-ever 5G-capable smartphone, is selling well. We expect sentiment to remain positive.",
"source_name": "Seeking Alpha",
"date": "Thu, 24 Dec 2020 22:56:54 -0500",
"topics": [
"dividend"
],
"sentiment": "Positive",
"type": "Article",
"tickers": [
"AAPL"
]
},
{
"news_url": "",
"image_url": "",
"title": "Square's Jack Dorsey reportedly looking to acquire Jay-Z's Tidal",
"text": "CNBC's Kate Rooney reports on Square's plans to be a 'mega-app,' by launching a clothing line and potentially buying Tidal, a music service owned by Jay-Z.",
"source_name": "CNBC Television",
"date": "Thu, 24 Dec 2020 12:48:13 -0500",
"topics": [
"CEO",
"manda"
],
"sentiment": "Positive",
"type": "Video",
"tickers": [
"SQ"
]
},
{
"news_url": "",
"image_url": "",
"title": "Apple reportedly has plans for an electric car: Investors will see limited earnings impact: Analyst",
"text": "Yahoo Finance's Brian Sozzi breaks down the note from Goldman Sachs analyst Rod Hall about Apple's plans to produce an electric car. For 2020 election results please visit: Election results: https://www.yahoo.com/elections Subscribe to Yahoo Finance: About Yahoo Finance: At Yahoo Finance, you get free stock quotes, up-to-date news, portfolio management resources, international market data, social interaction and mortgage rates that help you manage your financial life.",
"source_name": "Yahoo Finance",
"date": "Thu, 24 Dec 2020 12:41:18 -0500",
"topics": [
"earnings",
"product"
],
"sentiment": "Neutral",
"type": "Video",
"tickers": [
"AAPL"
]
},
{
"news_url": "",
"image_url": "",
"title": "Apple Partnering With Tesla Is Just First Step, Says Ives",
"text": "Dec.24 -- Dan Ives of Wedbush says Apple partnering with Tesla to make a self-driving electric car would make sense. He appears on "Bloomberg The Open.",
"source_name": "Bloomberg Technology",
"date": "Thu, 24 Dec 2020 12:28:20 -0500",
"topics": [
"product"
],
"sentiment": "Neutral",
"type": "Video",
"tickers": [
"AAPL"
]
},

这是我的代码

from newsapi import NewsApiClient
import requests
import json
# Init
import winsound
import sqlite3
duration = 1000  # milliseconds
freq = 440  # Hz

response = requests.get("https://stocknewsapi.com/api/v1?tickers=AAPL,SQ,PLTR&items=50&date=last7days&token=myapikey")

def jprint(obj):
# create a formatted string of the Python JSON object
text = json.dumps(obj, sort_keys=True, indent=4)
print(text +' ff /n')

for i in response:    
jprint(response.json()['data'][i]['date'] +  '          title : ' + response.json()['data'][i]['title'] + " news " +  response.json()['data'][i]['text'] +  " sentiment " + response.json()['data'][i]['sentiment'] )

您不想在数据中循环吗?data是一个列表,response是一个字典,它有一个关键字,data。

for i in response['data'] :
... 

根据您的代码,您应该从更新循环

for i in response:    

for i in range( len( response.json()['data'] )):

并且您的代码将按原样工作(调整缩进后(

from newsapi import NewsApiClient
import requests
import json
# Init
import winsound
import sqlite3
duration = 1000  # milliseconds
freq = 440  # Hz

response = requests.get("https://stocknewsapi.com/api/v1?tickers=AAPL,SQ,PLTR&items=50&date=last7days&token=myapikey")

def jprint(obj):
# create a formatted string of the Python JSON object
text = json.dumps(obj, sort_keys=True, indent=4)
print(text +' ff /n')

for i in range( len( response.json()['data'] )):    
jprint(response.json()['data'][i]['date'] +  '          title : ' + response.json()['data'][i]['title'] + " news " +  response.json()['data'][i]['text'] +  " sentiment " + response.json()['data'][i]['sentiment'] )

但是你可以通过让生活变得更轻松

my_data=response.json()['data']
for i in range( len( my_data )):    
jprint(my_data[i]['date'] +  '          title : ' +my_data[i]['title'] + " news " +  my_data[i]['text'] +  " sentiment " + my_data[i]['sentiment'] )

基于json的数据和代码示例

import json
def jprint(obj):
# create a formatted string of the Python JSON object
text = json.dumps(obj, sort_keys=True, indent=4)
print(text +' ff /n')

x = '''
{
"data": [
{
"news_url": "",
"image_url": "",
"title": "Attractively-Priced Apple Has Phenomenal Dividend Growth Potential",
"text": "Apple offers investors tremendous capital appreciation and dividend growth potential, in our view. The company is a true financial powerhouse. Initial reports indicate Apple's new iPhone 12 offering, its first-ever 5G-capable smartphone, is selling well. We expect sentiment to remain positive.",
"source_name": "Seeking Alpha",
"date": "Thu, 24 Dec 2020 22:56:54 -0500",
"topics": [
"dividend"
],
"sentiment": "Positive",
"type": "Article",
"tickers": [
"AAPL"
]
},
{
"news_url": "",
"image_url": "",
"title": "Square's Jack Dorsey reportedly looking to acquire Jay-Z's Tidal",
"text": "CNBC's Kate Rooney reports on Square's plans to be a 'mega-app,' by launching a clothing line and potentially buying Tidal, a music service owned by Jay-Z.",
"source_name": "CNBC Television",
"date": "Thu, 24 Dec 2020 12:48:13 -0500",
"topics": [
"CEO",
"manda"
],
"sentiment": "Positive",
"type": "Video",
"tickers": [
"SQ"
]
},
{
"news_url": "",
"image_url": "",
"title": "Apple reportedly has plans for an electric car: Investors will see limited earnings impact: Analyst",
"text": "Yahoo Finance's Brian Sozzi breaks down the note from Goldman Sachs analyst Rod Hall about Apple's plans to produce an electric car. For 2020 election results please visit: Election results: https://www.yahoo.com/elections Subscribe to Yahoo Finance: About Yahoo Finance: At Yahoo Finance, you get free stock quotes, up-to-date news, portfolio management resources, international market data, social interaction and mortgage rates that help you manage your financial life.",
"source_name": "Yahoo Finance",
"date": "Thu, 24 Dec 2020 12:41:18 -0500",
"topics": [
"earnings",
"product"
],
"sentiment": "Neutral",
"type": "Video",
"tickers": [
"AAPL"
]
},
{
"news_url": "",
"image_url": "",
"title": "Apple Partnering With Tesla Is Just First Step, Says Ives",
"text": "Dec.24 -- Dan Ives of Wedbush says Apple partnering with Tesla to make a self-driving electric car would make sense. He appears on Bloomberg The Open.",
"source_name": "Bloomberg Technology",
"date": "Thu, 24 Dec 2020 12:28:20 -0500",
"topics": [
"product"
],
"sentiment": "Neutral",
"type": "Video",
"tickers": [
"AAPL"
]
}
]
}
'''
my_data = json.loads(x)
for i in range(len(my_data["data"])):
jprint(my_data['data'][i]['date'] + '          title : ' + my_data['data'][i]['title'] + " news " +
my_data['data'][i]['text'] + " sentiment " + my_data['data'][i]['sentiment'])

输出

"Thu, 24 Dec 2020 22:56:54 -0500          title : Attractively-Priced Apple Has Phenomenal Dividend Growth Potential news Apple offers investors tremendous capital appreciation and dividend growth potential, in our view. The company is a true financial powerhouse. Initial reports indicate Apple's new iPhone 12 offering, its first-ever 5G-capable smartphone, is selling well. We expect sentiment to remain positive. sentiment Positive" ff /n
"Thu, 24 Dec 2020 12:48:13 -0500          title : Square's Jack Dorsey reportedly looking to acquire Jay-Z's Tidal news CNBC's Kate Rooney reports on Square's plans to be a 'mega-app,' by launching a clothing line and potentially buying Tidal, a music service owned by Jay-Z. sentiment Positive" ff /n
"Thu, 24 Dec 2020 12:41:18 -0500          title : Apple reportedly has plans for an electric car: Investors will see limited earnings impact: Analyst news Yahoo Finance's Brian Sozzi breaks down the note from Goldman Sachs analyst Rod Hall about Apple's plans to produce an electric car. For 2020 election results please visit: Election results: https://www.yahoo.com/elections Subscribe to Yahoo Finance: About Yahoo Finance: At Yahoo Finance, you get free stock quotes, up-to-date news, portfolio management resources, international market data, social interaction and mortgage rates that help you manage your financial life. sentiment Neutral" ff /n
"Thu, 24 Dec 2020 12:28:20 -0500          title : Apple Partnering With Tesla Is Just First Step, Says Ives news Dec.24 -- Dan Ives of Wedbush says Apple partnering with Tesla to make a self-driving electric car would make sense. He appears on Bloomberg The Open. sentiment Neutral" ff /n

最新更新