Python网抓 2021年 获取全部沪深港股ETF股票信息 东方财富
东方财富有最全(?)的股票信息,通过代码获取沪深股票, 港股股票, ETF股票信息,然后合并, 最后保存为csv文件。
环境python 3.8/3.9
def get_stock_data():
""" 获取沪深,港股股票和ETF数据"""
hs_url = """http://30.push2.eastmoney.com/api/qt/clist/get?cb=jQuery112404691234712152057_1632486404129&pn=1&pz=10000&po=1&np=1&ut=bd1d9ddb04089700cf9c27f6f7426281&fltt=2&invt=2&fid=f3&fs=m:0+t:6,m:0+t:80,m:1+t:2,m:1+t:23&fields=f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f12,f13,f14,f15,f16,f17,f18,f20,f21,f23,f24,f25,f22,f11&_=1632486404168"""
hk_url = """http://30.push2.eastmoney.com/api/qt/clist/get?cb=jQuery112408558330786963286_1632573181397&pn=1&pz=10000&po=1&np=1&ut=bd1d9ddb04089700cf9c27f6f7426281&fltt=2&invt=2&fid=f3&fs=m:128+t:3,m:128+t:4,m:128+t:1,m:128+t:2&fields=f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f12,f13,f14,f15,f16,f17,f18,f20,f21,f23,f24,f25,f22,f11&_=1632573181415"""
etf_url = 'http://94.push2.eastmoney.com/api/qt/clist/get?cb=jQuery112405026607085365067_1633009443039&pn=1&pz=1000&po=1&np=1&ut=bd1d9ddb04089700cf9c27f6f7426281&fltt=2&invt=2&fid=f3&fs=b:MK0021,b:MK0022,b:MK0023,b:MK0024&fields=f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f12,f13,f14,f15,f16,f17,f18,f20,f21,f23,f24,f25,f22,f11&_=1633009443081'
#response = requests.get(url, headers=header)
#沪深股票
hs_response = requests.get(hs_url)
hs_text = hs_response.text
hs_compile_data = "[" + re.findall("\\[(.*)\\]", str(hs_text))[0] + "]"
hs_df = pd.read_json(hs_compile_data)
#港股股票
hk_response = requests.get(hk_url)
hk_text = hk_response.text
hk_compile_data = "[" + re.findall("\\[(.*)\\]", str(hk_text))[0] + "]"
hk_df = pd.read_json(hk_compile_data)
#print(compile_data)
#ETF
etf_response = requests.get(etf_url)
etf_text = etf_response.text
etf_compile_data = "[" + re.findall("\\[(.*)\\]", str(etf_text))[0] + "]"
etf_df = pd.read_json(etf_compile_data)
#print(compile_data)
#合并沪深和港股股票
df = pd.concat([hs_df, hk_df, etf_df]).reset_index(drop=True)
column_list = ['', '最新价', '涨跌幅', '涨跌额', '成交量(手)', '成交额', '振幅', '换手率', '市盈率(动态)', '量比', '5分钟涨跌', '代码', '所属交易所', '名称', '最高价', '最低价', '开盘价', '总市值', '流通市值', '涨速', '市净率', '60日涨跌幅','年初至今涨跌幅','主力净流入']
df.columns=column_list
return df
if __name__ == '__main__':
stock_data = get_stock_data()
print(">>>>> The stock data as below: \n", stock_data)
stock_data.to_csv("hs_stocak_all.csv",encoding='utf_8_sig', index=False)
stock_data.to_csv("stocak.csv",encoding='utf_8_sig', index=False)