gpt4 book ai didi

javascript - 将 HTML 响应转换为数组或 NodeJS 中的对象

转载 作者:行者123 更新时间:2023-12-02 21:34:58 26 4
gpt4 key购买 nike

我编写了一个 Express 服务器,它可以访问 Zaubacorp.com API 并得到以下结果。结果是一个字符串

API 结果:

                <div class="show" align="left" id="company/CHOUDH/U65999MP1985PTC003043">CHOUDH </div>

<div class="show" align="left" id="company/CHOPRA/U45201CT1982PTC001899">CHOPRA </div>

<div class="show" align="left" id="company/CHOUDH/U72200UP1996PLC019258">CHOUDH </div>

<div class="show" align="left" id="company/CHOROSEN-LIMITED/U99999MH1955PLC009659">CHOROSEN LIMITED </div>

<div class="show" align="left" id="company/CHOLAI-FOUNDATION/U85300TN2019NPL127317">CHOLAI FOUNDATION </div>

现在,在获得 API 后,我需要将其发送到 React 前端。我需要从中提取特定值,例如 ID 和公司名称,并将其发送到 ReactJS 前端。我无法从给定的字符串格式中提取值。

这是我的 Express Controller

post_a_product =  async (req, res, next)=>{
console.log(req.body.query);
const query = {
"search": req.body.query,
"filter":"company"
}
var result = await axios.post("https://www.zaubacorp.com/custom-search", query)

stringData = result.data
try {
console.log('====================================');
console.log(stringData);
console.log('====================================');

} catch (error) {
console.error(error)
}
res.end("We have sent the response")

如何以某种格式从给定字符串中提取 ID 和公司名称以发送到前端?

最佳答案

您需要使用一些 html 解析器。其中之一是 https://www.npmjs.com/package/node-html-parser 。代码如下所示

const {parse} = require('node-html-parser');
console.log(JSON.stringify(parse(result.data)));

响应将是一个正确的 JSON,例如

{
"childNodes": [{
"childNodes": [{
"childNodes": [],
"nodeType": 3,
"rawText": "CHOUDH "
}],
"tagName": "div",
"rawAttrs": "class=\"show\" align=\"left\" id=\"company/CHOUDH/U65999MP1985PTC003043\"",
"parentNode": null,
"classNames": ["show"],
"nodeType": 1,
"id": "company/CHOUDH/U65999MP1985PTC003043"
}, {
"childNodes": [],
"nodeType": 3,
"rawText": "\n\t\t\t\t\t\t\t\n\t\t\t\t"
}, {
"childNodes": [{
"childNodes": [],
"nodeType": 3,
"rawText": "CHOPRA "
}],
"tagName": "div",
"rawAttrs": "class=\"show\" align=\"left\" id=\"company/CHOPRA/U45201CT1982PTC001899\"",
"parentNode": null,
"classNames": ["show"],
"nodeType": 1,
"id": "company/CHOPRA/U45201CT1982PTC001899"
}, {
"childNodes": [],
"nodeType": 3,
"rawText": "\n\t\t\t\t\t\t\t\n\t\t\t\t"
}, {
"childNodes": [{
"childNodes": [],
"nodeType": 3,
"rawText": "CHOUDH "
}],
"tagName": "div",
"rawAttrs": "class=\"show\" align=\"left\" id=\"company/CHOUDH/U72200UP1996PLC019258\"",
"parentNode": null,
"classNames": ["show"],
"nodeType": 1,
"id": "company/CHOUDH/U72200UP1996PLC019258"
}, {
"childNodes": [],
"nodeType": 3,
"rawText": "\n\t\t\t\t\t\t\t\n\t\t\t\t"
}, {
"childNodes": [{
"childNodes": [],
"nodeType": 3,
"rawText": "CHOROSEN LIMITED "
}],
"tagName": "div",
"rawAttrs": "class=\"show\" align=\"left\" id=\"company/CHOROSEN-LIMITED/U99999MH1955PLC009659\"",
"parentNode": null,
"classNames": ["show"],
"nodeType": 1,
"id": "company/CHOROSEN-LIMITED/U99999MH1955PLC009659"
}, {
"childNodes": [],
"nodeType": 3,
"rawText": "\n\t\t\t\t\t\t\t\n\t\t\t\t"
}, {
"childNodes": [{
"childNodes": [],
"nodeType": 3,
"rawText": "CHOLAI FOUNDATION "
}],
"tagName": "div",
"rawAttrs": "class=\"show\" align=\"left\" id=\"company/CHOLAI-FOUNDATION/U85300TN2019NPL127317\"",
"parentNode": null,
"classNames": ["show"],
"nodeType": 1,
"id": "company/CHOLAI-FOUNDATION/U85300TN2019NPL127317"
}],
"tagName": null,
"rawAttrs": "",
"parentNode": null,
"classNames": [],
"nodeType": 1,
"valid": true
}

这里有您需要的所有必要信息。希望这会有所帮助。

关于javascript - 将 HTML 响应转换为数组或 NodeJS 中的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60524177/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com