当Google重新开始行动时,我正试图为学校开展一个人事项目(股市预测)...
我了解到Google财务在过去的一年中一直是垃圾,但是直到今天早上它似乎仍在工作。 即使昨天运行良好,我还是第一次运行该代码。
因此,我尝试仅从实际的库页面运行示例代码:https://pypi.org/project/googlefinance.client/
!pip install googlefinance.client
from googlefinance.client import get_price_data, get_prices_data, get_prices_time_data
# Dow Jones
param = {
'q': ".DJI", # Stock symbol (ex: "AAPL")
'i': "86400", # Interval size in seconds ("86400" = 1 day intervals)
'x': "INDEXDJX", # Stock exchange symbol on which stock is traded (ex: "NASD")
'p': "1Y" # Period (Ex: "1Y" = 1 year)
}
# get price data (return pandas dataframe)
df = get_price_data(param)
print(df)
params = [
# Dow Jones
{
'q': ".DJI",
'x': "INDEXDJX",
},
# NYSE COMPOSITE (DJ)
{
'q': "NYA",
'x': "INDEXNYSEGIS",
},
# S&P 500
{
'q': ".INX",
'x': "INDEXSP",
}
]
period = "1Y"
# get open, high, low, close, volume data (return pandas dataframe)
df = get_prices_data(params, period)
print(df)
仍然得到
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-2-df3429694fd0> in <module>()
9 }
10 # get price data (return pandas dataframe)
---> 11 df = get_price_data(param)
12 print(df)
13
/usr/local/lib/python3.6/dist-packages/googlefinance/client.py in get_price_data(query)
13 cols = price.split(",")
14 if cols[0][0] == 'a':
---> 15 basetime = int(cols[0][1:])
16 index.append(datetime.fromtimestamp(basetime))
17 data.append([float(cols[4]), float(cols[2]), float(cols[3]), float(cols[1]), int(cols[5])])
ValueError: invalid literal for int() with base 10: 'nd ...</span><br></div></div><div class="g"><h3 class="r"><a href="/url?q=https://en.wikipedia.org/wiki/DJI_(company)&sa=U&ved=0ahUKEwiB-e_gjMzcAhUpwlkKHTTUC74QFghGMAw&usg=AOvVaw1ugw
以前有没有人碰过这个,知道怎么了或如何解决?
或者,另外,有人知道Google财务是一个很好的选择吗?
答案 0 :(得分:1)
示例代码有问题。如果您转到GitHub Homepage,则将获得最新版本-即使是小的更新。
我对client.py
进行了少许修改,但输出没有问题。
#!/usr/bin/env python
# coding: utf-8
import requests
from datetime import datetime
import pandas as pd
def get_price_data(query):
r = requests.get(
"https://finance.google.com/finance/getprices", params=query)
lines = r.text.splitlines()
data = []
index = []
basetime = 0
for price in lines:
cols = price.split(",")
if cols[0][0] == 'a':
basetime = int(cols[0][1:])
index.append(datetime.fromtimestamp(basetime))
data.append([float(cols[4]), float(cols[2]), float(
cols[3]), float(cols[1]), int(cols[5])])
elif cols[0][0].isdigit():
date = basetime + (int(cols[0]) * int(query['i']))
index.append(datetime.fromtimestamp(date))
data.append([float(cols[4]), float(cols[2]), float(
cols[3]), float(cols[1]), int(cols[5])])
return pd.DataFrame(data, index=index, columns=['Open', 'High', 'Low', 'Close', 'Volume'])
def get_closing_data(queries, period):
closing_data = []
for query in queries:
query['i'] = 86400
query['p'] = period
r = requests.get(
"https://finance.google.com/finance/getprices", params=query)
lines = r.text.splitlines()
data = []
index = []
basetime = 0
for price in lines:
cols = price.split(",")
if cols[0][0] == 'a':
basetime = int(cols[0][1:])
date = basetime
data.append(float(cols[1]))
index.append(datetime.fromtimestamp(date).date())
elif cols[0][0].isdigit():
date = basetime + (int(cols[0]) * int(query['i']))
data.append(float(cols[1]))
index.append(datetime.fromtimestamp(date).date())
s = pd.Series(data, index=index, name=query['q'])
closing_data.append(s[~s.index.duplicated(keep='last')])
return pd.concat(closing_data, axis=1)
def get_open_close_data(queries, period):
open_close_data = pd.DataFrame()
for query in queries:
query['i'] = 86400
query['p'] = period
r = requests.get(
"https://finance.google.com/finance/getprices", params=query)
lines = r.text.splitlines()
data = []
index = []
basetime = 0
for price in lines:
cols = price.split(",")
if cols[0][0] == 'a':
basetime = int(cols[0][1:])
date = basetime
data.append([float(cols[4]), float(cols[1])])
index.append(datetime.fromtimestamp(date).date())
elif cols[0][0].isdigit():
date = basetime + (int(cols[0]) * int(query['i']))
data.append([float(cols[4]), float(cols[1])])
index.append(datetime.fromtimestamp(date).date())
df = pd.DataFrame(data, index=index, columns=[
query['q'] + '_Open', query['q'] + '_Close'])
open_close_data = pd.concat(
[open_close_data, df[~df.index.duplicated(keep='last')]], axis=1)
return open_close_data
def get_prices_data(queries, period):
prices_data = pd.DataFrame()
for query in queries:
query['i'] = 86400
query['p'] = period
r = requests.get(
"https://finance.google.com/finance/getprices", params=query)
lines = r.text.splitlines()
data = []
index = []
basetime = 0
for price in lines:
cols = price.split(",")
if cols[0][0] == 'a':
basetime = int(cols[0][1:])
date = basetime
data.append([float(cols[4]), float(cols[2]), float(
cols[3]), float(cols[1]), int(cols[5])])
index.append(datetime.fromtimestamp(date).date())
elif cols[0][0].isdigit():
date = basetime + (int(cols[0]) * int(query['i']))
data.append([float(cols[4]), float(cols[2]), float(
cols[3]), float(cols[1]), int(cols[5])])
index.append(datetime.fromtimestamp(date).date())
df = pd.DataFrame(data, index=index, columns=[
query['q'] + '_Open', query['q'] + '_High', query['q'] + '_Low', query['q'] + '_Close', query['q'] + '_Volume'])
prices_data = pd.concat(
[prices_data, df[~df.index.duplicated(keep='last')]], axis=1)
return prices_data
def get_prices_time_data(queries, period, interval):
prices_time_data = pd.DataFrame()
for query in queries:
query['i'] = interval
query['p'] = period
r = requests.get(
"https://finance.google.com/finance/getprices", params=query)
lines = r.text.splitlines()
data = []
index = []
basetime = 0
for price in lines:
cols = price.split(",")
if cols[0][0] == 'a':
basetime = int(cols[0][1:])
date = basetime
data.append([float(cols[4]), float(cols[2]), float(
cols[3]), float(cols[1]), int(cols[5])])
index.append(datetime.fromtimestamp(date))
elif cols[0][0].isdigit():
date = basetime + (int(cols[0]) * int(query['i']))
data.append([float(cols[4]), float(cols[2]), float(
cols[3]), float(cols[1]), int(cols[5])])
index.append(datetime.fromtimestamp(date))
df = pd.DataFrame(data, index=index, columns=[
query['q'] + '_Open', query['q'] + '_High', query['q'] + '_Low', query['q'] + '_Close', query['q'] + '_Volume'])
prices_time_data = pd.concat(
[prices_time_data, df[~df.index.duplicated(keep='last')]], axis=1)
return prices_time_data
代码段
params = {
'q': ".DJI", # Stock symbol (ex: "AAPL")
'i': "86400", # Interval size in seconds ("86400" = 1 day intervals)
# Stock exchange symbol on which stock is traded (ex: "NASD")
'x': "INDEXDJX",
'p': "1Y" # Period (Ex: "1Y" = 1 year)
}
df = get_price_data(params)
print(df)
输出
交易量高开...关闭
328405532 2017-08-01 15:00:00 21961.42 21990.96 ... 21963.92
328405532 2017-08-02 15:00:00 22004.36 22036.10 ... 22016.24
336824836 2017-08-03 15:00:00 22007.58 22044.85 ... 22026.10
278731064 2017-08-04 15:00:00 22058.39 22092.81 ... 22092.81
253635270 2017-08-07 15:00:00 22100.20 22121.15 ... 22118.42
213012378 2017-08-08 15:00:00 22095.14 22179.11 ... 22085.34
答案 1 :(得分:0)
最近48个小时左右,".INX"
尚未在我的Google表格上更新。 .DJI
和.IXIC
仍在更新中,尽管我认为其中之一已经有一段时间了。