首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >为什么python无法读取雅虎财经API?

为什么python无法读取雅虎财经API?
EN

Stack Overflow用户
提问于 2021-07-22 11:38:50
回答 1查看 66关注 0票数 0

我有以下代码来分析雅虎财经的股票价格,但我总是遇到错误。

代码语言:javascript
复制
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import pandas_datareader as web
import datetime as dt


from sklearn.preprocessing import MinMaxScaler 
from tensorflow.keras.models import Sequential 
from tensorflow.keras.layers import Dense, Dropout, LSTM


company = str(input('enter the company symbol '))
company = 'FB'
start = dt.datetime(2011,1,1)
end = dt.datetime(2019,1,1)

data = web.DataReader(company, 'yahoo', start, end)

scaler = MinMaxScaler(feature_range=(0,1))
scaled_data = scaler.fit_transform(data['Close'].values.reshape(-1,1))
prediction_days = 60

x_train = []
y_train = []

for x in range (prediction_days, len(scaled_data)):
  x_train.append(scaled_data[x-prediction_days:x,0])
  y_train.append(scaled_data[x,0])

x_train, y_train = np.array(x_train), np.array(y_train)
x_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1],1))

model = Sequential()
model.add(LSTM(units=50, return_sequences=True, input_shape =(x_train.shape[1],1)))
model.add(Dropout(0.2))
model.add(LSTM(units=50, return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(units=50))
model.add(Dropout(0.2))
model.add(Dense(units=1))

model.compile(optimizer='adam', loss='mean_squared_error')
model.fit(x_train, y_train, epochs=25, batch_size=32)

test_start=dt.datetime(2019,1,1)
test_end = dt.datetime.now()

test_data = web.DataReader(company, 'yahoo', test_start, test_end)
actual_prices = test_data['Close'].values

total_dataset = pd.concat((data['Close'],test_data['Close']), axis=0)
model_inputs = total_dataset[len(total_dataset)- len(test_data) - prediction_days:].values
model_inputs = model_inputs.reshape(-1,1)
model_inputs = scaler.transform(model_inputs)


x_test = []

for x in range(prediction_days, len(model_inputs)):
  x_test.append(model_inputs[x-prediction_days:x,0])

x_test = np.array(x_test)
x_test = np.reshape(x_test, (x_test.shape[0],x_test.shape[1],1))

predicted_prices = model.predict(x_test)
predicted_prices = scaler.inverse_transform(predicted_prices)

plt.plot(actual_prices, color = 'black', label = f"Actual{company}  price")
plt.plot(predicted_prices, color = 'green', label = f"Predicted{company}  price")
plt.title(f"{company}  share price")
plt.xlabel ('Time')
plt.ylabel(f"{company}  share price")
plt.legend()
plt.show()

我得到以下错误

代码语言:javascript
复制
RemoteDataError: Unable to read URL: https://finance.yahoo.com/quote/FB/history?                
period1=1293854400&period2=1546401599&interval=1d&frequency=1d&filter=history
Response Text:
b'<!DOCTYPE html>\n  <html lang="en-us"><head>\n  <meta http-equiv="content-type" 
content="text/html; charset=UTF-8">\n      <meta charset="utf-8">\n      
<title>Yahoo</title>\n      <meta name="viewport" content="width=device-width,initial- 
scale=1,minimal-ui">\n      <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">\n      
<style>\n  html {\n      height: 100%;\n  }\n  body {\n      background: #fafafc 
url(https://s.yimg.com/nn/img/sad-panda-201402200631.png) 50% 50%;\n      background-size: 
cover;\n      height: 100%;\n      text-align: center;\n      font: 300 18px "helvetica neue", 
helvetica, verdana, tahoma, arial, sans-serif;\n  }\n  table {\n      height: 100%;\n      
width: 100%;\n      table-layout: fixed;\n      border-collapse: collapse;\n      border- 
spacing: 0;\n      border: none;\n  }\n  h1 {\n      font-size: 42px;\n      font-weight: 
400;\n      color: #400090;\n  }\n  p {\n      color: #1A1A1A;\n  }\n  #message-1 {\n      
font-weight: bold;\n      margin: 0;\n  }\n  #message-2 {\n      display: inline-block;\n      
*display: inline;\n      zoom: 1;\n      max-width: 17em;\n      _width: 17em;\n  }\n               
</style>\n  <script>\n    document.write(\'<img src="//geo.yahoo.com/b?s=1197757129&t=\'+new 
Date().getTime()+\'&src=aws&err_url=\'+encodeURIComponent(document.URL)+\'&err=% 
<pssc>&test=\'+encodeURIComponent(\'%<{Bucket}cqh[:200]>\')+\'" width="0px" 
height="0px"/>\');var 
beacon = new Image();beacon.src="//bcn.fp.yahoo.com/p?s=1197757129&t="+ne...

我知道API之前有问题,但我以为现在已经解决了。有没有其他方法可以从API中提取数据?任何关于如何解决这个问题的建议都将不胜感激。不知道为什么我一直收到这个。这在几周前还能很好地工作。另外,我使用GoogleColab来运行这段代码。

有什么建议吗?

EN

回答 1

Stack Overflow用户

发布于 2021-07-22 13:21:39

雅虎财经改为yfinance。所以pip install yfinance然后你可以这样做

代码语言:javascript
复制
 import yfinance as yf
 yf.pdr_override() 
 from pandas_datareader import data as pdr
 temp_df = pdr.get_data_yahoo("FB", start, end)

它返回一个数据帧temp_df

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68478870

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档