我正在尝试复制他在2013年作为教程上传的Sentdex的股票筛选器(https://www.youtube.com/watch?v=Y4GHgJjIQnk)。不幸的是,此后发生了很多变化,因此需要对他提出的代码进行较小的调整。我仅在下面发布部分无法正常工作的代码。如果有人感兴趣,可以通过上面的链接获得完整的代码。
以下是该代码,基本上是原始代码,但与yahoo url方法有关,但有一些次要例外:
import urllib.request
import time
import datetime
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
import matplotlib.dates as mdates
import mpl_finance
from mpl_finance import candlestick_ohlc
import matplotlib
import pylab
...
def graphData(stock,MA1,MA2):
'''
Use this to dynamically pull a stock:
'''
try:
print('Currently Pulling',stock)
urlToVisit = 'https://query1.finance.yahoo.com/v8/finance/chart/'+stock+'?interval=2m'
stockFile =[]
try:
sourceCode = urllib.request.urlopen(urlToVisit).read().decode()
splitSource = sourceCode.split('\n')
for eachLine in splitSource:
splitLine = eachLine.split(',')
if len(splitLine)==6:
if 'values' not in eachLine:
stockFile.append(eachLine)
except Exception as e:
print(str(e), 'failed to organize pulled data.')
except Exception as e:
print(str(e), 'failed to pull pricing data')
try:
date, closep, highp, lowp, openp, volume = np.loadtxt(stockFile,delimiter=',', unpack=True, converters={0: bytespdate2num('%Y%m%d')})
我得到的输出如下:
Currently Pulling ABT
UserWarning: loadtxt: Empty input file: "[]"
date, closep, highp, lowp, openp, volume = np.loadtxt(stockFile,delimiter=',', unpack=True, converters={0: bytespdate2num('%Y%m%d')})
list assignment index out of range
Currently Pulling ABBV
main loop list assignment index out of range
在所有相关进口之后指定的sp500列表中的所有代码的... etc。
有什么想法吗?我尝试从中提取信息的链接如下:https://query1.finance.yahoo.com/v8/finance/chart/AAPL?interval=2m
答案 0 :(得分:3)
问题在于您处理Yahoo
的响应的方式,更精确地说:
splitSource = sourceCode.split('\n')
并没有您所想的那样,处理输出的最佳和最简便的方法是使用json.loads()
,因为Yahoo输出已经采用json
格式:>
import urllib.request
import time
import datetime
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
import matplotlib.dates as mdates
import mpl_finance
from mpl_finance import candlestick_ohlc
import matplotlib
import pylab, json
...
def graphData(stock,MA1,MA2):
'''
Use this to dynamically pull a stock:
'''
try:
print('Currently Pulling',stock)
urlToVisit = 'https://query1.finance.yahoo.com/v8/finance/chart/'+stock+'?interval=2m'
print(urlToVisit)
stockFile =[]
try:
sourceCode = urllib.request.urlopen(urlToVisit).read().decode()
sourceCode = json.loads(sourceCode)
print(sourceCode['chart']['result'][0]['indicators']['quote'][0].keys())
#dict_keys(['low', 'close', 'open', 'high', 'volume'])
...
只需浏览嵌套字典
json.loads
返回,您就可以按照自己喜欢的顺序获取任何数据:
stock = "FB"
print('Currently Pulling',stock)
urlToVisit = 'https://query1.finance.yahoo.com/v8/finance/chart/'+stock+'?interval=2m'
print(urlToVisit)
stockFile =[]
sourceCode = urllib.request.urlopen(urlToVisit).read().decode()
sourceCode = json.loads(sourceCode)
resp_ = {}
resp_ = sourceCode['chart']['result'][0]['indicators']['quote'][0]
resp_['timestamp'] = sourceCode['chart']['result'][0]['timestamp']
print(resp_.keys())
#dict_keys(['close', 'open', 'timestamp', 'volume', 'high', 'low'])
以及每个值的时间戳。