如何通过Excel VBA或Python获取目标网站的股票代码相关元数据
如何通过Excel VBA或Python获取目标网站的股票代码相关元数据
嗨,我帮你梳理了两种可行的方案,分别用VBA和Python来获取你需要的股票peer数据——你之前的问题在于请求了主页面,但实际数据存储在专门的API接口中,主页面的HTML里并没有包含这些内容,所以直接请求API接口就可以拿到数据啦:
一、VBA实现方案
方式1:使用VBA-JSON库解析(推荐)
这种方式兼容性更好,适合处理复杂JSON结构,你需要先下载并导入VBA-JSON模块到你的VBA项目中。
Sub GetStockPeersData() Dim xhr As MSXML2.ServerXMLHTTP60 Dim jsonResponse As Object Dim peers As Variant Dim peer As Variant Dim rowIndex As Integer ' 直接请求存储数据的API接口 Const API_URL As String = "https://www.screener.in/api/company/13611040/peers/" Set xhr = New MSXML2.ServerXMLHTTP60 With xhr .Open "GET", API_URL, False ' 模拟浏览器请求头,避免被拦截 .SetRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36" .Send If .ReadyState = 4 And .Status = 200 Then ' 解析JSON响应 Set jsonResponse = JsonConverter.ParseJson(.responseText) ' 提取peers节点下的所有股票数据 Set peers = jsonResponse("peers") ' 将数据输出到Excel工作表(从A1单元格开始) rowIndex = 1 For Each peer In peers Cells(rowIndex, 1).Value = peer("name") ' 公司名称 Cells(rowIndex, 2).Value = peer("symbol") ' 股票代码 rowIndex = rowIndex + 1 Next peer Else MsgBox "请求失败,状态码:" & .Status End If End With ' 释放对象 Set xhr = Nothing Set jsonResponse = Nothing Set peers = Nothing End Sub
方式2:使用ScriptControl解析(无需外部库)
如果不想导入外部模块,可以用Excel自带的ScriptControl解析JSON,但注意该组件在64位Excel中可能存在兼容性问题:
Sub GetStockPeersData_ScriptControl() Dim xhr As MSXML2.ServerXMLHTTP60 Dim sc As Object Dim jsonResponse As Object Dim peers As Variant Dim peer As Variant Dim rowIndex As Integer Const API_URL As String = "https://www.screener.in/api/company/13611040/peers/" Set xhr = New MSXML2.ServerXMLHTTP60 With xhr .Open "GET", API_URL, False .SetRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36" .Send If .ReadyState = 4 And .Status = 200 Then ' 创建ScriptControl对象解析JSON Set sc = CreateObject("MSScriptControl.ScriptControl") sc.Language = "JScript" Set jsonResponse = sc.Eval("(" & .responseText & ")") ' 提取peers数组 Set peers = jsonResponse.peers ' 输出数据到Excel rowIndex = 1 For Each peer In peers Cells(rowIndex, 1) = peer.name Cells(rowIndex, 2) = peer.symbol rowIndex = rowIndex + 1 Next peer Else MsgBox "请求失败,状态码:" & .Status End If End With ' 释放对象 Set xhr = Nothing Set sc = Nothing Set jsonResponse = Nothing End Sub
VBA注意事项:
- 在VBA编辑器中,点击「工具」→「引用」,勾选
Microsoft XML, v6.0和Microsoft Scripting Runtime - 64位Excel用户建议优先选择VBA-JSON方案
二、Python实现方案
Python处理这类API请求和JSON解析会更简洁,只需要用到requests库(如果要导出Excel还需要pandas):
基础版(打印数据)
import requests # 目标API地址 api_url = "https://www.screener.in/api/company/13611040/peers/" # 模拟浏览器请求头 headers = { "User-Agent": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36" } # 发送GET请求 response = requests.get(api_url, headers=headers) if response.status_code == 200: # 解析JSON数据 data = response.json() peers = data.get("peers", []) # 打印股票代码和公司名称 print("股票代码 | 公司名称") print("--------|-------------------") for peer in peers: print(f"{peer['symbol']:^8} | {peer['name']}") else: print(f"请求失败,状态码:{response.status_code}")
进阶版(导出到Excel)
如果需要把数据保存到Excel文件,安装pandas和openpyxl库后执行以下代码:
import requests import pandas as pd api_url = "https://www.screener.in/api/company/13611040/peers/" headers = {"User-Agent": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36"} response = requests.get(api_url, headers=headers) if response.status_code == 200: data = response.json() # 将peers数据转换为DataFrame df = pd.DataFrame(data["peers"]) # 导出到Excel df.to_excel("股票关联数据.xlsx", index=False, engine="openpyxl") print("数据已成功导出到 股票关联数据.xlsx") else: print(f"请求失败,状态码:{response.status_code}")
Python注意事项:
- 先通过
pip install requests pandas openpyxl安装所需依赖库
备注:内容来源于stack exchange,提问作者Kiran




