【问题标题】:Node.js: how to create an array of specific objects based on data from html string?Node.js:如何根据 html 字符串中的数据创建特定对象的数组?
【发布时间】:2021-07-04 12:00:57
【问题描述】:

我是 Node.js 的初学者,出于测试目的,我想创建一个简单的应用程序,根据给定的 HTML 创建一个对象数组。

让我解释一下:我有一个包含多个 div 元素的 HTML 字符串,如下所示:

<div class="user_container">
    <div class="user">
        <div class="thumb">
            <!--            thumbnail block-->
        </div>
        <div class="web_presence_locations"></div>

        <div class="user_data">
            <span class="name">Jaroslaw Chujczynski</span>
            <p class="location_with_flag">
                <!--                img with url here-->
                Leeds,
                United Kingdom
            </p>
            <div class="user_details">
                <div class="amount currency">
                    £28,000.00
                    <span class="overbooked">(in overfunding)</span>
                </div>
            </div>
        </div>
    </div>
    <div class="profile_container">
        <div class="extra_profile_data" style="">
            <div class="investments last">
                <h3 class="h5">Recent Investments</h3>
                <ul>
                    <li class="first">
                        <div class="campaign-logo-frame">
                            <a class="campaign_link" href="/test1">test1</a>
                            <span class="currency">£28,000.00</span>
                        </div>
                    </li>
                    <li class="">
                        <div class="campaign-logo-frame">
                            <a class="campaign_link" href="/test2">test2</a>
                            <span class="currency">£28,000.00</span>
                        </div>
                    </li>
                    <li class="">
                        <div class="campaign-logo-frame">
                            <a class="campaign_link" href="/test3">test3</a>
                            <span class="currency">£28,000.00</span>
                        </div>
                    </li>
                    <li class="">
                        <div class="campaign-logo-frame">
                            <a class="campaign_link" href="/test4">test4</a>
                            <span class="currency">£28,000.00</span>
                        </div>
                    </li>
                </ul>
            </div>
        </div>
    </div>
</div>

我想要做的是根据我在上面的 div 中的数据创建一个对象,例如它会是这样的:

{
name: 'Jaroslaw Chujczynski',
location: 'Leeds, United Kingdom',
amountCurrency: '£28,000.00 (in overfunding)',
lastInvestments: [
 {
  name: 'test1',
  currency: '£28,000.00'
 }, {
  name: 'test2',
  currency: '£28,000.00'
 }, {
  name: 'test3',
  currency: '£28,000.00'
 }, {
  name: 'test4',
  currency: '£28,000.00'
 }]
}

当然,在我的 html 中会有很多这样的 div,所以我将创建一个此类对象的数组。

好吧,我现在有什么:

const fs = require('fs');
const cheerio = require('cheerio');

const getAllData = (fileName) => {
    try {
        return  fs.readFileSync(fileName, 'utf8');
    } catch(e) {
        console.log('Error:', e.stack);
    }
}
const data = getAllData('test.html');
const $ = cheerio.load(data);

const filterData = () => {
    console.log($('div[class="user_container"]'));
}

filterData();

它返回给我的东西是这样的——那是不需要的(或者它必须是这样的?):

 namespace: 'http://www.w3.org/1999/xhtml',
    attribs: [Object: null prototype] {
      class: 'user_container'
    },
    'x-attribsNamespace': [Object: null prototype] {
      class: undefined
    },
    'x-attribsPrefix': [Object: null prototype] {
      class: undefined
    },
    children: [ [Node], [Node], [Node], [Node], [Node], [Node] ],
    parent: Node {
      type: 'tag',
      name: 'section',
      namespace: 'http://www.w3.org/1999/xhtml',
      attribs: [Object: null prototype],
      'x-attribsNamespace': [Object: null prototype],
      'x-attribsPrefix': [Object: null prototype],
      children: [Array],
      parent: [Node],
      prev: [Node],
      next: [Node]
    },
    etc....

所以我不确定,但首先我必须得到一个 div 块数组,其中类是user_container,当我得到它时,我必须遍历这个数组来为每个数组创建对象。

有人可以帮我解决这个问题吗?

【问题讨论】:

  • 这个问题需要更多的关注。您要做的第一件事是从 HTML 中获取值,所以我会先寻找答案,而不是问这样一个特定的问题。

标签: javascript html node.js cheerio


【解决方案1】:

html 是一种 XML——您应该查看 XML 工具——让该工具解析 html,然后您可以使用该工具对它们运行 XML 查询。这将允许您提取可以转换为 JSON 的 XML。

快速谷歌搜索返回以下用于 nodejs 的 XML 工具——但还有更多:

https://www.npmjs.com/package/fast-xml-parser - 表示它也会导出为 JSON

http://www.curtismlarson.com/blog/2018/10/03/edit-xml-node-js/ - 周四有详细的步行。

【讨论】:

  • 不,cheerio 是你想要的。
  • @pguardiario --cheerio 不是解析 html 的 nodejs 工具吗?
  • 是的,这就是他的要求。
  • @pguardiario -- 所以我在回答中说他想要一个解析 html 的 nodejs 工具 -- 对吗?
  • 是的,但他正在寻求一种 Cheerio 解决方案,出于某种原因,您建议使用其他东西。 Cheerio 是解决此问题的正确工具。
【解决方案2】:

我至少可以让你开始:

const data = $('.user_container').get().map(div => {
  return {
    name: $(div).find('.name').text(),
    location: $(div).find('.location_with_flag').text(),
    amountCurrency: $(div).find('.amount.currency').text(),
  }
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    相关资源
    最近更新 更多