本文模仿akshare中的方法,利用python的request提取东方财富网站的期权、期货数据,可用于量化研究,
代码如下:
import json
import requests
import pandas as pd
#输出显示设置
import warnings
pd.set_option('display.max_rows',None)
pd.set_option('display.max_columns',None)
pd.set_option('expand_frame_repr',False)
pd.set_option('display.unicode.ambiguous_as_wide',True)
pd.set_option('display.unicode.east_asian_width',True)
warnings.filterwarnings("ignore")
def option_current_em() -> pd.DataFrame:
"""
东方财富网-行情中心-期权市场
https://quote.eastmoney.com/center/qqsc.html
:return: 期权价格
:rtype: pandas.DataFrame
"""
url = 'http://77.push2.eastmoney.com/api/qt/clist/get'
params = {
'pn': '1',
'pz': '200000',
'po': '1',
'np': '1',
'ut': 'bd1d9ddb04089700cf9c27f6f7426281',
'fltt': '2',
'invt': '2',
'fid': 'f3',
'fs': 'm:10,m:140,m:141,m:151',
'fields': 'f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f12,f13,f14,f15,f16,f17,f18,f20,f21,f23,f24,f25,f22,f28,f11,f62,f128,f136,f115,f152,f133,f108,f163,f161,f162',
'_': '1606225274063',
}
r = requests.get(url, params=params)
data_json = r.json()
temp_df = pd.DataFrame(data_json['data']['diff'])
temp_df.columns = [
'_',
'最新价',
'涨跌幅',
'涨跌额',
'成交量',
'成交额',
'_',
'_',
'_',
'_',
'_',
'代码',
'市场标识',
'名称',
'_',
'_',
'今开',
'_',
'_',
'_',
'_',
'_',
'_',
'_',
'昨结',
'_',
'持仓量',
'_',
'_',
'_',
'_',
'_',
'_',
'_',
'行权价',
'剩余日',
'日增'
]
temp_df = temp_df[[
'代码',
'名称',
'最新价',
'涨跌额',
'涨跌幅',
'成交量',
'成交额',
'持仓量',
'行权价',
'剩余日',
'日增',
'昨结',
'今开',
'市场标识',
]]
temp_df['最新价'] = pd.to_numeric(temp_df['最新价'], errors='coerce')
temp_df['涨跌额'] = pd.to_numeric(temp_df['涨跌额'], errors='coerce')
temp_df['涨跌幅'] = pd.to_numeric(temp_df['涨跌幅'], errors='coerce')
temp_df['成交量'] = pd.to_numeric(temp_df['成交量'], errors='coerce')
temp_df['成交额'] = pd.to_numeric(temp_df['成交额'], errors='coerce')
temp_df['持仓量'] = pd.to_numeric(temp_df['持仓量'], errors='coerce')
temp_df['行权价'] = pd.to_numeric(temp_df['行权价'], errors='coerce')
temp_df['剩余日'] = pd.to_numeric(temp_df['剩余日'], errors='coerce')
temp_df['日增'] = pd.to_numeric(temp_df['日增'], errors='coerce')
temp_df['昨结'] = pd.to_numeric(temp_df['昨结'], errors='coerce')
temp_df['今开'] = pd.to_numeric(temp_df['今开'], errors='coerce')
return temp_df
def future_current_em(market='上期所') -> pd.DataFrame:
"""
东方财富网-行情中心-期货市场-上期所
https://quote.eastmoney.com/center/gridlist2.html#futures_113
:return: 期货价格
:rtype: pandas.DataFrame
"""
mk_dict={'上期所':113,'大商所':114,'郑商所':115,'上期能源':142,'中金所':220}
market_code=mk_dict[market]
url = 'https://futsseapi.eastmoney.com/list/{}'.format(market_code)
params = { #拷贝payload(负载)参数如下:
'callback': 'aaa_callback',
'orderBy': 'zdf',
'sort': 'desc',
'pageSize': '20000',#设置一个足够大数据,一次提取所有数据
'pageIndex': '0',#如果非0,修改为0,一次提取所有数据
'callbackName': 'aaa_callback',
'token': '58b2fa8f54638b60b87d69b31969089c', #可以删除,不影响
'field': 'dm,sc,name,p,zsjd,zde,zdf,f152,o,h,l,zjsj,vol,cje,wp,np,ccl',
'blockName': 'callback', #可以删除,不影响
'_': '1714487033740', #可以删除,不影响
}
r = requests.get(url, params=params)
jsonDecoder=json.decoder.JSONDecoder()
data=jsonDecoder.decode(r.text[13:-1])['list']
temp_df = pd.DataFrame(data)
temp_df.columns = [#原始列名顺序
'卖盘(内盘)', '最高','代码','zsjd','最低','持仓量','今开','最新价',
'市场标识','成交量','名称','买盘(外盘)','涨跌额','涨跌幅','昨结','成交额',
]
temp_df=temp_df[['代码','名称','最新价','涨跌额','涨跌幅','今开','最高','最低','昨结',
'成交量','成交额','买盘(外盘)','卖盘(内盘)','持仓量']]#重新排列名顺序
return temp_df
if __name__ == '__main__':
df=future_current_em(market='上期所')
print(df)
df_option=option_current_em()
print(df_option.head())