python爬取期权行情数据

想要弄点数据要求高的可以找收费的数据服务商,例如wind,东方财富,后者便宜点,tushare也提供了期权行情数据,但是门槛是有积分限制,其他的地方只能爬取了。做期权策略分析没有数据怎么行,没钱就写一个吧。

数据源

新浪财经:https://stock.finance.sina.com.cn/option/quotes.html

python爬取期权行情数据_第1张图片

主要涉及的接口

当前交易的期权合约月份

http://stock.finance.sina.com.cn/futures/api/openapi.php/StockOptionService.getStockName

{"result":{"status":{"code":0},"data":{"cateList":["50ETF","50ETF","300ETF"],"contractMonth":["2020-06","2020-06","2020-07","2020-09","2020-12"],"stockId":"510050","cateId":"510050C2006A02800"}}}

当前标的月份下的期权合约

http://hq.sinajs.cn/list=OP_UP_5100502006

var hq_str_OP_UP_5100502006="CON_OP_10002421,CON_OP_10002401,CON_OP_10002402,CON_OP_10002291,CON_OP_10002292,CON_OP_10002293,CON_OP_10002294,CON_OP_10002083,CON_OP_10002009,CON_OP_10002084,CON_OP_10001989,CON_OP_10002085,CON_OP_10001990,CON_OP_10002086,CON_OP_10001991,CON_OP_10002087,CON_OP_10001992,CON_OP_10002088,CON_OP_10001993,CON_OP_10002089,CON_OP_10001994,CON_OP_10002090,CON_OP_10001995,CON_OP_10002091,CON_OP_10001996,CON_OP_10002107,CON_OP_10001997,CON_OP_10002115,CON_OP_10002007,CON_OP_10002213,";

当前期权合约行情

http://hq.sinajs.cn/list=CON_OP_10002086

var hq_str_CON_OP_10002086="10,0.0614,0.0615,0.0615,24,102344,15.38,2.8500,0.0533,0.0550,0.3411,0.0001,0.0619,3,0.0618,26,0.0617,3,0.0616,1,0.0615,24,0.0614,10,0.0613,25,0.0612,65,0.0611,5,0.0610,161,2020-06-05 15:00:00,0,E 01,EBS,510050,50ETF购6月2850,29.83,0.0615,0.0456,121255,63182460.00,M,0.0533,C,2020-06-24,19,2,0.042,0.0195";

python实现

import requests
import json
from itertools import product
import pandas as pd

get_option_months_api = "http://stock.finance.sina.com.cn/futures/api/openapi.php/StockOptionService.getStockName"
get_option_quot = 'http://hq.sinajs.cn/list='
underlying = ['510050', '510300']
call_put_type = ['OP_UP_', 'OP_DOWN_']
col = ['买量', '买价', '最新价', '卖价', '卖量', '持仓量', '涨跌幅', '行权价', '昨收', '今开', '涨停', '跌停', '卖5价', '卖5量', '卖4价', '卖4量', '卖3价',
       '卖3量', '卖2价', '卖2量', '卖1价', '卖1量', '买1价', '买1量', '买2价', '买2量', '买3价', '买3量', '买4价', '买4量', '买5价', '买5量', '闭市时段',
       '未知', '未知', '未知', '标的', '名称', '振幅', '时间价值', '最低价', '成交量', '未知', '未知', '昨结算', '类型', '行权日', '距离行权', '未知', '内在价值',
       '未知', '编码']


def get_contract_months():
    """
    获取合约月
    :return:
    """
    resp = requests.get(get_option_months_api)
    result = json.loads(resp.text)
    months = result['result']['data']['contractMonth']
    format_funtion = lambda month: str(month).replace("-", '')[2:]
    months = [format_funtion(month) for month in months]
    return list(set(months))


def get_contracts(months):
    """
    获取合约月
    :return:
    """
    result = {}
    resp = requests.get('{}{}'.format(get_option_quot, ','.join(months)))
    lines = resp.text.splitlines()
    for line in lines:
        temp = line.split('=')
        key_name = temp[0].split('_')[-1]
        value = temp[1].split('\"')[1].split(',')[:-1]
        if key_name in result:
            result[key_name].extend(value)
        else:
            result[key_name] = value
    return result


def get_contract_quot(month_contract, writer):
    """
    获取行情详情
    :param month_contract:
    :param writer:
    :return:
    """
    for key, value in month_contract.items():
        resp = requests.get('{}{}'.format(get_option_quot, ','.join(value)))
        result_temp = []
        lines = resp.text.splitlines()
        for line in lines:
            temp = line.split('=')
            code = temp[0].split('_')[-1]
            value = temp[1].split('\"')[1].split(',')
            value.append(code)
            result_temp.append(value)
        df = pd.DataFrame(result_temp, columns=col)
        df.to_excel(writer, key)


if __name__ == '__main__':
    contract_months = get_contract_months()
    underlying_months_param = ['{}{}{}'.format(call_put_type_item, underlying_item, contract_month) for
                               call_put_type_item, underlying_item, contract_month in
                               product(call_put_type, underlying, contract_months)]
    print(underlying_months_param)
    month_contract = get_contracts(underlying_months_param)
    print(month_contract)
    writer = pd.ExcelWriter('output.xlsx')
    get_contract_quot(month_contract=month_contract, writer=writer)
    writer.save()

结果如下

python爬取期权行情数据_第2张图片

其他可用的接口

A股股票&基金

http://hq.sinajs.cn/list=sh601006

http://hq.sinajs.cn/list=sh502007

A股指数

http://hq.sinajs.cn/list=s_sz399001

港股股票

http://hq.sinajs.cn/list=hk02333

http://hq.sinajs.cn/list=rt_hkCSCSHQ #沪港通资金流量

港股指数

http://hq.sinajs.cn/list=int_hangseng

http://hq.sinajs.cn/list=rt_hkHSI

http://hq.sinajs.cn/list=hkHSI,hkHSCEI,hkHSCCI #恒生指数,恒生国企指数,恒生红筹指数

美股股票&基金

http://hq.sinajs.cn/list=gb_amzn

http://hq.sinajs.cn/list=usr_amzn

http://hq.sinajs.cn/list=usr_russ

美股指数

http://hq.sinajs.cn/list=int_nasdaq

http://hq.sinajs.cn/list=gb_ixic #纳斯达克指数

http://hq.sinajs.cn/list=int_dji

http://hq.sinajs.cn/list=int_sp500

http://hq.sinajs.cn/list=int_ftse #伦敦指数

http://hq.sinajs.cn/list=int_bloombergeuropean500 #彭博欧洲500指数

http://hq.sinajs.cn/list=int_dax30,int_djstoxx50

外汇行情

http://hq.sinajs.cn/list=XAUUSD

http://hq.sinajs.cn/list=DINIW #美元指数

黄金&白银

http://hq.sinajs.cn/list=hf_XAU

http://hq.sinajs.cn/list=hf_XAG

http://hq.sinajs.cn/list=hf_GC #COMEX黄金

http://hq.sinajs.cn/list=hf_SI #COMEX白银

http://hq.sinajs.cn/list=hf_AUTD #黄金TD

http://hq.sinajs.cn/list=hf_AGTD #白银TD

http://hq.sinajs.cn/list=AU0 #黄金期货

http://hq.sinajs.cn/list=AG0 #白银期货

http://hq.sinajs.cn/list=hf_CL #NYMEX原油

期货

http://hq.sinajs.cn/list=CFF_LIST #金融期货合约

http://finance.sina.com.cn/iframe/futures_info_cff.js #商品与金融期货合约

http://hq.sinajs.cn/?list=CFF_RE_IF1705 #合约行情

期权合约的月份

http://stock.finance.sina.com.cn/futures/api/openapi.php/StockOptionService.getStockName

期权合约到期日

http://stock.finance.sina.com.cn/futures/api/openapi.php/StockOptionService.getRemainderDay?date=201705

看涨期权合约

http://hq.sinajs.cn/list=OP_UP_5100501705

看跌期权合约

http://hq.sinajs.cn/list=OP_DOWN_5100501705

期权行情

http://hq.sinajs.cn/list=CON_OP_10000869

http://hq.sinajs.cn/list=CON_ZL_10000869

http://hq.sinajs.cn/list=CON_SO_10000869

热门股票

http://finance.sina.com.cn/realstock/company/hotstock_daily_a.js

新股日历

http://vip.stock.finance.sina.com.cn/corp/view/iframe/vAK_NewStockIssueFrame_2015.php?num=10

定增列表

http://vip.stock.finance.sina.com.cn/corp/view/vAK_IncreaseStockIssueFrame_2015.php?num=10

基金公司

http://vip.stock.finance.sina.com.cn/fund_center/api/jsonp.php/var%20companyList=/NetValue_Service.getAllCompany

 

你可能感兴趣的:(python,爬虫,python)