还是python的新手。我从纳斯达克下载了所有股票。
import ftplib
import os
import re
# Connect to ftp.nasdaqtrader.com
ftp = ftplib.FTP('ftp.nasdaqtrader.com', 'anonymous', 'anonymous@debian.org')
# Download files nasdaqlisted.txt and otherlisted.txt from ftp.nasdaqtrader.com
for ficheiro in ["nasdaqlisted.txt", "otherlisted.txt"]:
ftp.cwd("/SymbolDirectory")
localfile = open(ficheiro, 'wb')
ftp.retrbinary('RETR ' + ficheiro, localfile.write)
localfile.close()
ftp.quit()
# Grep for common stock in nasdaqlisted.txt and otherlisted.txt
for ficheiro in ["nasdaqlisted.txt", "otherlisted.txt"]:
localfile = open(ficheiro, 'r')
for line in localfile:
if re.search("Common Stock", line):
ticker = line.split("|")[0]
# Append tickers to file tickers.txt
open("tickers.txt","a+").write(ticker + "\n")
我想利用这个ticker.txt文件,并在yfinance中循环遍历该文件,以找到可能的股息收益率和市盈率。我得到这个:IndexError:列表索引超出范围。我想解决这个问题。我该如何解决这个问题,并使用纳斯达克的所有股票以及股息收益率和市盈率来创建输出?下面是我的第二个脚本。
import bs4
import requests
from bs4 import BeautifulSoup
import yfinance as yf
dictionary = {}
with open("tickers.txt") as f:
stocks = f.read().splitlines()
for t in stocks:
ticker = yf.Ticker(t)
#dictionary[t] = ticker.info["priceToBook"]
dictionary[t] = ticker.info["dividendYield"]
#dictionary[t] = get_pe_ratio(t)
print(dictionary)
f = open("DivYield.txt", "w+")
f.write( (t+1))