You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过jsdom获取Roblox文档中特定类的段落文本内容?

问题

尝试从Roblox开发者文档(以Axes数据类型为例)获取数据类型的摘要信息,使用jsdom工具编写了代码,但当前脚本输出大量参数相关元素,无法获取目标摘要文本:

The 'Axes' datatype is for the 'ArcHandles' class to control which rotation axes are currently enabled.

现有代码如下:

const Jsdom = require('jsdom');
const { JSDOM } = Jsdom;
const Axios = require('axios');

async function Data_descriptions(Data_types) {
    let Descriptions = [];

    for (let Index = 0; Index < Data_types.length; Index++) {
        const Data_type = Data_types[Index];
        const Description = await Axios.get(
            `https://create.roblox.com/docs/reference/engine/datatypes/${Data_type}`,
            {
                responseType : 'document'
            }
        ).then(Response => Response.data).then(Data => new JSDOM(Data)).then(Body => Array.from( Body.window.document.querySelectorAll('span.MuiTypography-root.jss15') ));

        Descriptions.push(Description);
    }

    return Descriptions;
}

Data_descriptions(['Axes']).then(Descriptions => {
    console.log( Descriptions [ 0 ] );
    
    Descriptions [ 0 ].forEach(
        Element => console.log( `Text: ${Element.text} | Content: ${Element.textContent} | Inner: ${Element.innerText}` )
    );
})
解决方案

问题出在你选择的CSS选择器span.MuiTypography-root.jss15不对,目标摘要文本所在的元素是页面开头的段落,对应的正确选择器可以定位到该段落元素。

修改后的代码如下:

const Jsdom = require('jsdom');
const { JSDOM } = Jsdom;
const Axios = require('axios');

async function Data_descriptions(Data_types) {
    let Descriptions = [];

    for (let Index = 0; Index < Data_types.length; Index++) {
        const Data_type = Data_types[Index];
        const Description = await Axios.get(
            `https://create.roblox.com/docs/reference/engine/datatypes/${Data_type}`,
            {
                responseType : 'document'
            }
        ).then(Response => Response.data)
          .then(Data => new JSDOM(Data))
          .then(dom => {
              // 定位目标描述段落,根据页面DOM结构调整选择器
              const descElement = dom.window.document.querySelector('.jss14 p');
              return descElement ? descElement.textContent.trim() : 'No description found';
          });

        Descriptions.push(Description);
    }

    return Descriptions;
}

Data_descriptions(['Axes']).then(Descriptions => {
    console.log(Descriptions[0]);
})

关键说明

  • 原选择器匹配的是页面中大量参数相关的文本元素,并非目标摘要。
  • 新选择器直接定位到目标摘要所在的段落元素,通过textContent获取文本并去除多余空格,即可得到需要的内容。
  • 增加了兜底处理,若找不到目标元素会返回提示文本,避免程序报错。

内容的提问来源于stack exchange,提问作者Diaxium

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 14:09:25