python爬取期权行情数据

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

数据源

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

主要涉及的接口

当前交易的期权合约月份

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()

结果如下

其他可用的接口

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

 

  • 10
    点赞
  • 60
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
好的,您想了解如何用 Python 爬取历史天气数据,是吗? 首先,需要确定您想要爬取哪个地区的历史天气数据。一般来说,可以通过查询该地区的气象局或气象网站获取历史天气数据。 接着,可以使用 Python 中的 requests 库来发送 HTTP 请求,获取网页源代码。然后,可以使用 BeautifulSoup 库来解析网页源代码,提取出所需的天气数据。 以下是一个简单的示例代码,用于爬取中国天气网上北京市 2019 年 1 月份的历史天气数据: ``` python import requests from bs4 import BeautifulSoup url = 'http://www.weather.com.cn/weather/101010100.shtml#dt' headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'} r = requests.get(url, headers=headers) r.encoding = 'utf-8' soup = BeautifulSoup(r.text, 'html.parser') weather_list = soup.select('.t li') for weather in weather_list: date = weather.select('.time')[0].text weather_detail = weather.select('.temp')[0].text.strip() print(date, weather_detail) ``` 以上代码中,我们首先通过 requests 库发送了一个 GET 请求,获取了北京市天气网的网页源代码。然后,使用 BeautifulSoup 库解析网页源代码,提取出了 2019 年 1 月份的天气数据,并打印输出了日期和天气详情。 需要注意的是,不同的网站网页结构不同,需要根据具体情况修改代码。同时,需要注意网站的 robots.txt 文件,不要过度访问网站,以免被封 IP 或其他限制。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值