Ctrl+U,查看页面源代码,
找到期权合约月份:Option_select: http://stock.finance.sina.com.cn/futures/api/openapi.php/StockOptionService.getStockName
1 {"result":{"status":{"code":0},"data":{"cateList":["50ETF","50ETF","300ETF"],"contractMonth":["2022-06","2022-06","2022-07","2022-09","2022-12"],"stockId":"510050","cateId":"510050C2206A02950"}}}
F12,打开开发者工具模式,待自动更新后,我们发现要提取的链接:
查看标头,URL :https://hq.sinajs.cn/list=CON_ZL_10004288
这是单个期权合约的信息,但是当我打开这个链接时,出现如下情况: Kinsoku jikou desu! (日本语禁止访问)
应该是反爬虫措施,把请求标头这一堆加上再试试
运行后正常显示:
1 import requests 2 3 url = 'https://hq.sinajs.cn/list=CON_ZL_10004288' 4 5 headers = { 6 7 'Host': 'hq.sinajs.cn', 8 'Referer': 'https://stock.finance.sina.com.cn', 9 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.63 Safari/537.36 Edg/102.0.1245.30' 10 } 11 12 r = requests.get( url, headers=headers, timeout = 5) 13 14 print(r.text)
下面,只有找到所有期权的代码,然后在在网址“https://hq.sinajs.cn/list=”+“期权代码”即可找到所有期权的信息啦
我们发现这一堆数据和期权行权价数据个数相等,应该就是期权认沽或认购期权列表了,提取其代码进行测试即可