import urllib
import re
import pandas as pd
import os
from io import StringIO, BytesIO
import time
def getHtml(url):
html = urllib.request.urlopen(url).read()
html = html.decode('gbk')
return html
Url163 = 'http://quotes.money.163.com/service/chddata.html?code=%s&start=19910102&end=%s&fields=TCLOSE;HIGH;LOW;TOPEN;LCLOSE;CHG;PCHG;TURNOVER;VOTURNOVER;VATURNOVER;TCAP;MCAP'
stock_id='1000001'
url = Url163%(stock_id,time.strftime("%Y%m%d", time.localtime()) )
aa=getHtml(url)
df = pd.read_csv(StringIO(aa))
df.to_excel(stock_id+'.xlsx')
stock_id='0600000'
url = Url163%(stock_id,time.strftime("%Y%m%d", time.localtime()) )
aa=getHtml(url)
df = pd.read_csv(StringIO(aa))
df.to_excel(stock_id+'.xlsx')
本文介绍了一种使用Python从网易财经获取股票历史数据的方法。通过定义URL格式和使用urllib库,可以请求特定股票代码的历史数据,然后利用pandas读取并保存为Excel文件。示例中展示了如何为两个不同股票ID获取数据。
432

被折叠的 条评论
为什么被折叠?



