1

下面的代码适用于美国股票 APLE 和 BHP,但是当我用 ASX 代码替换它们时,它会崩溃。我虽然这是由于冒号,但没有成功放置 str(ASX:BHP)。不幸的是,雅虎不再提供历史数据。任何想法或解决方案的替代品将不胜感激。

谢谢

import datetime
import pandas as pd
from pandas_datareader import data, wb

list = ["APLE","BHP"] 
#list = ["ASX:AMP","ASX:BHP"] 
df_all_stock = pd.DataFrame([])

start = datetime.datetime(2016, 1, 1)
end = datetime.datetime(2017, 1, 1)

for row in list:

    row = str(row)
    df_stock = data.DataReader(row, "google", start, end)
    df_all_stock = df_all_stock.append(df_stock)
    df_all_stock['code'] = row

df_all_stock
4

3 回答 3

0

将需要构建一个刮板以从 html 表中获取数据,然后构建一个类似于我们作为美国股票数据输出的 pandas 数据框。

我确定谷歌金融上加拿大股票的基本网址是:' https://www.google.ca/finance/historical?q=TSE%3A '要获取股票的数据,我们只需将其名称附加到末尾上述基本网址。例如,要查看“VCN”的历史股票数据,我们需要转到以下页面:https ://www.google.ca/finance/historical?q=TSE%3AVCN

要在 python 代码中执行上述操作,我们只需要以下内容,其中可以为任何感兴趣的 TSE(Tornto 证券交易所)股票更改股票变量。

from datetime import datetime
from pandas import DataFrame
import pandas_datareader.data as web
google_historical_price_site= 'https://www.google.ca/finance/historical?
q=TSE%3A'
stock = 'VCN' #sub any sock in here
historical_price_page = google_historical_price_site + stock
print(historical_price_page)


from urllib.request import urlopen
from bs4 import BeautifulSoup

#open the historical_price_page link and acquire the source code
stock_dat = urlopen(historical_price_page)
#parse the code using BeautifulSoup
historical_page = BeautifulSoup(stock_dat,'lxml')

#scrape the table
table_dat = historical_page.find('table',{'class':'gf-table 
historical_price'})
#find all the rows in the table
rows = table_dat.findAll('td',{'class':'lm'})
#get just the dates out of the table rows, strip the newline characters
dates = [x.get_text().rstrip() for x in rows]
#turn dates to python datetime format
datetime_dates = [datetime.strptime(x, '%b %d, %Y') for x in dates]

#next we build up the price dataframe rows
#iterate through the table, taking the siblings to the 
#right of the dates and adding to the row's data
prices = []
for num, row in enumerate(rows):
row_dat = [datetime_dates[num]] #first column is the dates
for i in row.next_siblings: 
row_dat.append(i.get_text().rstrip()) #iterate through columns, append
prices.append(row_dat) #add the row to the list of rows
#turn the output into the dataframe
outdat = DataFrame(prices,columns = 
['Date','Open','High','Low','Close','Volume'])
#make the Volume columns integers, in case we wish to use it later!
outdat["Volume"] = outdat["Volume"].apply(lambda x: int(x.replace(',','')))
#change the other columns to floating point values
for col in ['Open','High','Low','Close']:
outdat[col] = outdat[col].apply(lambda x: float(x))
#set the index to match the american stock data
outdat = outdat.set_index('Date')
#sort the index so it is in the same orientation as the american data
outdat = outdat.sort_index() 

#have a look
outdat
于 2017-10-20T00:54:41.883 回答
0

以 CSV 文件格式下载港股的示例(股票示例:腾讯控股有限公司(HKG:0700)

from datetime import datetime
from pandas import DataFrame
import pandas_datareader.data as web
import os

google_historical_price_site='https://finance.google.com/finance/historical?q=HKG:0700'
print(google_historical_price_site)

from urllib.request import urlopen
from bs4 import BeautifulSoup

#open the historical_price_page link and acquire the source code
stock_dat = urlopen(google_historical_price_site)
#parse the code using BeautifulSoup
google_historical_price_site = BeautifulSoup(stock_dat,'lxml')

#scrape the table
table_dat = google_historical_price_site.find('table',{'class':'gf-table 
historical_price'})
#find all the rows in the table
rows = table_dat.findAll('td',{'class':'lm'})
#get just the dates out of the table rows, strip the newline characters
dates = [x.get_text().rstrip() for x in rows]   
#turn dates to python datetime format
datetime_dates = [datetime.strptime(x, '%b %d, %Y') for x in dates]

#next we build up the price dataframe rows 
#iterate through the table, taking the siblings to the 
#right of the dates and adding to the row's data
prices = []
for num, row in enumerate(rows):
row_dat = [datetime_dates[num]] #first column is the dates
for i in row.next_siblings: 
row_dat.append(i.get_text().rstrip()) #iterate through columns, append
prices.append(row_dat) #add the row to the list of rows
#turn the output into the dataframe
outdat = DataFrame(prices,columns = 
['Date','Open','High','Low','Close','Volume'])
#make the Volume columns integers, in case we wish to use it later!
outdat["Volume"] = outdat["Volume"].apply(lambda x: int(x.replace(',','')))
#change the other columns to floating point values
for col in ['Open','High','Low','Close']:
outdat[col] = outdat[col].apply(lambda x: float(x))
#set the index to match the american stock data
outdat = outdat.set_index('Date')
#sort the index so it is in the same orientation as the american data
outdat = outdat.sort_index() 

#output CSV.file 
df=outdat
path_d = 'C:\MA data'
df.to_csv(os.path.join(path_d, 'HKGstock700.csv'))
于 2017-10-20T01:50:40.540 回答
0

只需使用 ASX API

https://www.asx.com.au/asx/1/share/AMP

将返回:

{"code":"AMP","isin_code":"AU000000AMP6","desc_full":"Ordinary Fully Paid","last_price":1.155,"open_price":1.115,"day_high_price":1.155,"day_low_price":1.11,"change_price":0.040,"change_in_percent":"3.587%","volume":24558498,"bid_price":1.15,"offer_price":1.16,"previous_close_price":1.115,"previous_day_percentage_change":"3.241%","year_high_price":1.77,"last_trade_date":"2021-08-13T00:00:00+1000","year_high_date":"2020-12-03T00:00:00+1100","year_low_price":1.038,"year_low_date":"2021-07-30T00:00:00+1000","year_open_price":4.97,"year_open_date":"2014-02-25T11:00:00+1100","year_change_price":-3.815,"year_change_in_percentage":"-76.761%","pe":32.08,"eps":0.036,"average_daily_volume":20511519,"annual_dividend_yield":0,"market_cap":3641708026,"number_of_shares":3266105853,"deprecated_market_cap":3772352000,"deprecated_number_of_shares":3266105853,"suspended":false}

其他可以为您提供价格历史、公告、董事、股息等的示例查询:

https://www.asx.com.au/asx/1/share/AMP/prices?interval=daily&count=255
https://www.asx.com.au/asx/1/company/AMP
https://www.asx.com.au/asx/1/company/AMP?fields=primary_share,latest_annual_reports,last_dividend,primary_share.indices
https://www.asx.com.au/asx/1/company/AMP/announcements?count=10&market_sensitive=true
https://www.asx.com.au/asx/1/company/AMP/dividends
https://www.asx.com.au/asx/1/company/AMP/dividends/history?years=10
https://www.asx.com.au/asx/1/company/AMP/people
https://www.asx.com.au/asx/1/company/AMP/options?count=1000
https://www.asx.com.au/asx/1/company/AMP/warrants?count=1000
https://www.asx.com.au/asx/1/chart/highcharts?asx_code=AMP&years=10
https://www.asx.com.au/asx/1/company/AMP/similar?compare=marketcap

我在这里包含了一些示例 Python 代码:https ://stackoverflow.com/a/68790147/8459557

于 2021-08-15T09:15:51.317 回答