使用 3 个索引访问字典项目列表(YFinance 新闻下载)

问题描述 投票:0回答:2

我正在努力创建一个程序,每次运行时都会为我的投资组合中的每只股票生成今天的新闻。我已经成功下载了投资组合股票的新闻,据我所知,它是以字典列表的形式呈现的。这是 AAPL 今天新闻的示例输出,因此您可以看到格式: 这是使用标题 [0] 创建的(标题包含所有新闻文章及其参数)

[{'uuid': '5be16b13-a550-3b19-bfc7-6db8f78ae01c',
  'title': "Apple to host annual developers' conference from June 5",
  'publisher': 'Reuters',
  'link': 'https://finance.yahoo.com/news/apple-host-annual-developers-conference-173217646.html',
  'providerPublishTime': 1680111137,
  'type': 'STORY',
  'thumbnail': {'resolutions': [{'url': 'https://s.yimg.com/uu/api/res/1.2/vTSqzTi3XOE4.NVOu2KeKQ--~B/aD01MzM7dz04MDA7YXBwaWQ9eXRhY2h5b24-/https://media.zenfs.com/en/reuters-finance.com/c52dd0fd2a1bb097651c5fa27fc7a3d9',
     'width': 800,
     'height': 533,
     'tag': 'original'},
    {'url': 'https://s.yimg.com/uu/api/res/1.2/F4MkRNXpiOMZVqhwa4V9ug--~B/Zmk9ZmlsbDtoPTE0MDtweW9mZj0wO3c9MTQwO2FwcGlkPXl0YWNoeW9u/https://media.zenfs.com/en/reuters-finance.com/c52dd0fd2a1bb097651c5fa27fc7a3d9',
     'width': 140,
     'height': 140,
     'tag': '140x140'}]},
  'relatedTickers': ['AAPL']}

AAPL一共有8篇文章,每只股票的文章数不一样。 我只对提取每只股票的文章标题感兴趣。

我的代码格式如下:

import os
import matplotlib.pyplot as plt
from bs4 import BeautifulSoup
import pandas as pd
from urllib.request import urlopen, Request
from nltk.sentiment.vader import SentimentIntensityAnalyzer
import yfinance as yf

tickers = ['AAPL', 'ABB', 'AIG', 'AMR', 'CB', 'COLD', 'COOP', 'COST', 'CRM', 'CVS', 'DBOEY', 'DG', 'DIS', 'ENPH', 'F', 'GOOG',
          'JPM', 'LMT', 'LULU', 'LVMUY', 'MSFT', 'NHYDY', 'NKE', 'OXY', 'PG', 'QCOM', 'RTX', 'SBGSY', 'SHEL', 'STZ', 'TM',
          'TMUS', 'TSM', 'UPS', 'VEEV', 'WMT', 'ZTS']
yf_objects = []
for ticker in tickers:
    ticker = yf.Ticker(ticker)
    yf_objects.append(ticker)

headlines = []
for i in yf_objects:
    headlines.append(i.news)

到目前为止,我能够一次为一只股票的一个条目分离标题的唯一方法是:

headlines[0][0]['title']

返回 AAPL(第一个 0 索引)、第一篇文章(第二个 0 索引)和标题 ['title']。 但是,如果我必须为每只股票手动运行每篇文章,这将破坏这个小项目的意义。

我尝试过嵌套 for 循环但没有成功,非常感谢您帮助找到一种方法来遍历代码列表中的每个代码,每个代码的每篇文章,并且只提取标题。

任何帮助将不胜感激,我已经在这个问题上工作了两个小时(我是一个初学者编码员),一旦我弄清楚了这对我个人会有很大帮助。

谢谢!!

python nested iteration finance yfinance
2个回答
0
投票

你可以稍微简化你正在做的事情来获得一个字典,其中键是代码,值是文章列表。像这样的……

import yfinance as yf

output_dict = {}
tickers = ['AAPL', 'ABB', 'AIG', ...]
for ticker in tickers:
    yf_ticker = yf.Ticker(ticker)
    output_dict[ticker] = [news['title'] for news in yf_ticker.news]

给你...

{'AAPL': ['Today’s top stories: Bank regulator hearing, Starbucks CEO testimony, Apple Developers Conference',
  'Best Streaming Devices: Google Chromecast, Apple TV, Amazon Fire and More',
  'Apple announces its annual conference dates in June, hinting unveil of VR headset',
  'Apple Sets June Dates for Developer Conference',
  'Apple TV Could Allow Viewers To View Multiple Sports Simultaneously After Rival Launches Similar Feature',
  "Apple to host annual developers' conference from June 5",
  'How Microsoft And Google Beat Apple And Amazon To The Top In Generative AI',
  'Apple’s Worldwide Developers Conference returns June 5, 2023'],
 'ABB': ['ABB to Launch New Share Buyback Program of up to $1B in April',
  'ABB Invests $20M to Expand Robotics Facility in Auburn Hills',
  "Has ABB Ltd (VTX:ABBN) Stock's Recent Performance Got Anything to Do With Its Financial Health?",
  'ABB Ramps Up Investment In Its Robotics Facility In US',
  "ABB Shares Up 17% in 6 Months: What's Driving the Stock?",
  'ABB to expand Robotics factory in US',
  '11 Cheap EV Stocks To Buy',
  'ABB Collaborates With DEP to Promote Energy Transformation'],
 'AIG': ['Those who invested in American International Group (NYSE:AIG) three years ago are up 101%',
  'American International Group, Inc. (NYSE:AIG) insiders who sold US$918k worth of stock earlier this year are probably glad they did so as market cap slides to US$35b',
  'Should You Hold American International Group (AIG) For Long-Term Returns?',
  'Meta’s Layoffs Are Just a Drop in the Bucket. These Companies Cut More.',
  'From Bear Stearns to Credit Suisse: Crises, Mergers and Bailouts',
  'American International Group (AIG) Down 19.3% Since Last Earnings Report: Can It Rebound?',
  "American International Group, Inc.'s (NYSE:AIG) Fundamentals Look Pretty Strong: Could The Market Be Wrong About The Stock?",
  'William G. Jurgensen to Retire from the AIG Board of Directors'],
...
}

0
投票

不完全是最有效的,但希望容易理解:

# If this is what you have (removed irrelevant keys)
headlines = [
 {'title': "Apple to host annual developers' conference from June 5",
  'relatedTickers': ['AAPL']},
 {'title': "Title2",
  'relatedTickers': ['AAPL']},
 {'title': "Title3",
  'relatedTickers': ['AAPL', 'AMZN']},
]

# Sounds like you're looking for:
all_tickers = {ticker for headline in headlines for ticker in headline['relatedTickers']}
headlines_by_ticker = {
    ticker: [headline['title'] for headline in headlines if ticker in headline['relatedTickers']]
    for ticker in all_tickers
}

给出:

{'AMZN': ['Title3'],
 'AAPL': ["Apple to host annual developers' conference from June 5",
  'Title2',
  'Title3']}

由于您是初学者编码员,这里有一个不同的实现来做同样的事情,以防您发现这更容易理解。

from collections import defaultdict

headlines_by_ticker = defaultdict(list)
for headline in headlines:
    for ticker in headline['relatedTickers']:
        headlines_by_ticker[ticker].append(headline['title'])
© www.soinside.com 2019 - 2024. All rights reserved.