You need to enable JavaScript to run this app. VBA获取ChEMBL网页分子量时指定td类InnerText返回空如何解决
优惠活动
大模型
产品
解决方案
定价
更多

VBA获取ChEMBL网页分子量时指定td类InnerText返回空如何解决

问题原因
  • 你当前使用的MSXML2.XMLHTTP只能获取页面初始的静态HTML源码,ChEMBL的化合物报告页的分子量这类核心数据是页面加载完成后通过JavaScript调用后端接口动态渲染的,你拿到的静态源码里根本不存在Bck-FULLMWT这个元素,所以返回空值。
推荐解决方案:直接调用官方API获取数据(更稳定,不需要处理动态渲染)

ChEMBL公开了所有化合物的结构化数据接口,直接请求对应化合物的API接口,解析返回的JSON就能直接拿到分子量,比爬HTML页面效率高、稳定性强。
使用前准备:

  • 自行搜索导入VBA-JSON解析模块,无需额外安装软件
  • 引用Microsoft Scripting Runtime库

修改后的代码如下:

Dim request As Object
Dim response As String
Dim json As Object
Dim website As String
Dim MWT As Variant

' 直接调用ChEMBL公开的分子数据API,不需要请求HTML页面
website = "https://www.ebi.ac.uk/chembl/api/data/molecule/CHEMBL1112.json"
Set request = CreateObject("MSXML2.XMLHTTP.6.0")
request.Open "GET", website, False
request.setRequestHeader "Accept", "application/json"
request.send
response = StrConv(request.responseBody, vbUnicode)

' 解析JSON获取分子量
Set json = JsonConverter.ParseJson(response)
MWT = json("molecule_properties")("full_molformula_weight")

' 后续填充内容控件的逻辑不变
For Each CCv In ActiveDocument.ContentControls
    Select Case CCv.Title
        Case "Molecular wt"
            CCv.Range.Text = MWT
        Case "Molecular formula"
            CCv.Range.Text = json("molecule_properties")("full_molformula")
    End Select
Next CCv

Set request = Nothing
Set json = Nothing
End Sub
备选方案:使用IE自动化处理动态渲染

如果你一定要解析HTML页面元素,可以用IE对象加载页面,等待JS渲染完成后再取元素,代码示例:

Dim ie As Object
Dim html As HTMLDocument
Dim MWT As Variant
Dim website As String

website = "https://www.ebi.ac.uk/chembl/compound_report_card/CHEMBL1112/"
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = False ' 设为True可以看到IE窗口加载过程,方便调试
ie.navigate website

' 等待页面加载完成
Do While ie.readyState <> 4 Or ie.Busy
    DoEvents
Loop
' 额外等待1-2秒确保JS渲染完成,可根据实际情况调整时长
Application.Wait Now + TimeValue("00:00:02")

Set html = ie.document
MWT = html.getElementById("Bck-FULLMWT").innerText

' 填充内容控件逻辑不变
For Each CCv In ActiveDocument.ContentControls
    Select Case CCv.Title
        Case "Molecular wt"
            CCv.Range.Text = MWT
        Case "Molecular formula"
            CCv.Range.Text = "Type"
    End Select
Next CCv

ie.Quit
Set ie = Nothing
Set html = Nothing
End Sub

内容的提问来源于stack exchange,提问作者Abd El-Rahman Sayed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.22 05:15:02