gpt4 book ai didi

python - Google 财经错误 : invalid literal

转载 作者:行者123 更新时间:2023-12-01 09:11:14 26 4
gpt4 key购买 nike

我正试图为学校做一个个人项目(股票市场预测),这时 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&nbsp;...</span><br></div></div><div class="g"><h3 class="r"><a href="/url?q=https://en.wikipedia.org/wiki/DJI_(company)&amp;sa=U&amp;ved=0ahUKEwiB-e_gjMzcAhUpwlkKHTTUC74QFghGMAw&amp;usg=AOvVaw1ugw

有人以前遇到过这个问题并知道出了什么问题或如何解决吗?

或者,请问有人知道 Google 财经的良好替代方案吗?

最佳答案

这是示例代码的问题。如果您转到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)

输出

Volume Open High ... Close
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

关于python - Google 财经错误 : invalid literal,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51636537/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com