【问题标题】:Extract text from HTML elements and Create object [closed]从 HTML 元素中提取文本并创建对象 [关闭]
【发布时间】:2018-08-26 16:35:47
【问题描述】:

我正在尝试整理以下代码,我使用正则表达式并需要一些帮助。

这是我从网站获取后保存到变量中的文本。

[ '<td bgcolor="#2D2F34">&nbsp;<font color="#999999">Name: </font><a href="site.php?page=send&sendto=Username"><font color="#999999">Username</font></a>&nbsp;&nbsp;&nbsp;</td>', '<td bgcolor="#2D2F34">&nbsp;<font color="#999999">Crew: </font><a href="site.php?page=crewprofile&id=2120"><font color="#999999">My Crew</font></a> </td>', '<td bgcolor="#2D2F34">&nbsp;<font color="#999999">Wealth: Rich</font></td>', '<td bgcolor="#2D2F34">&nbsp;<font color="#999999">Rank: Hitman</td>', '<td bgcolor="#2D2F34">&nbsp;<font color="#999999">Status: Alive ( </font><font color=green>Online</font><font color="#999999"> )</font><tr><td bgcolor="#2D2F34">&nbsp;<font color="#999999">Messages sent: 3</font></td>', '<td bgcolor="#2D2F34">&nbsp;<font color="#999999">Messages received: 1</font></td>' ]

此文本也可以包含更多或更少的标签,因为这是从每个“个人资料”都不同的网站获取的。

我希望它返回的是

Name: Username   
Crew: My Crew   
Wealth: Rich   
Rank: Hitman
Status: Alive ( Online )
Messages sent: 3
Messages received: 1

感谢所有帮助!谢谢

【问题讨论】:

  • 使用 HTML 解析器,而不是正则表达式 (obligatory link)。 Node.js 有几个。
  • 为了改进您的问题,提供任何预期字符串匹配的完整超集以及您希望它们拥有的值将是有益的。同样正如@T.J.Crowder 已经说过的那样,使用 HTML 解析器也会更有效率。
  • @BhojendraRauniyar 请阅读:"...after fetching it from a website" - 所以还有希望
  • @JanHenning 正如建议的那样,用正则表达式解析 HTML 从来都不是一件好事。如果在 Node 中,请查看 npmjs.com/package/jsdom 并简单地从 Elements 中检索 textContent - 我建议首先创建一个内存 &lt;table&gt;&lt;tr&gt; 作为包装器,将 &lt;td&gt; 字符串作为元素附加到之前试图获取内容。
  • @RokoC.Buljan 谢谢,会看看。给您添麻烦了。

标签: javascript node.js regex


【解决方案1】:

您可以使用DocumentFragment&lt;td&gt; 元素中提取所需的数据。
对于 Node,看看一些像这样的助手:jsdom@npmjs

const td = [ '<td bgcolor="#2D2F34">&nbsp;<font color="#999999">Name: </font><a href="site.php?page=send&sendto=Username"><font color="#999999">Username</font></a>&nbsp;&nbsp;&nbsp;</td>', '<td bgcolor="#2D2F34">&nbsp;<font color="#999999">Crew: </font><a href="site.php?page=crewprofile&id=2120"><font color="#999999">My Crew</font></a> </td>', '<td bgcolor="#2D2F34">&nbsp;<font color="#999999">Wealth: Rich</font></td>', '<td bgcolor="#2D2F34">&nbsp;<font color="#999999">Rank: Hitman</td>', '<td bgcolor="#2D2F34">&nbsp;<font color="#999999">Status: Alive ( </font><font color=green>Online</font><font color="#999999"> )</font></td>', '<td bgcolor="#2D2F34">&nbsp;<font color="#999999">Messages sent: 3</font></td>', '<td bgcolor="#2D2F34">&nbsp;<font color="#999999">Messages received: 1</font></td>' ];

const tr = document.createElement("tr");
const table = document.createElement("table");
const frag = document.createDocumentFragment(); // Minimal Document wrapper

tr.innerHTML = td.join("");
table.appendChild(tr);
frag.appendChild(table);

const data = [...frag.querySelectorAll("td")].reduce((ob, td) => {
  const a = td.textContent.split(':');
  ob[a[0].trim()] = a.slice(1).join(":").trim();
  return ob;
}, {})

console.log( data );

PS:

!!!? 在您的数组中,您有一个 &lt;/font&gt;&lt;tr&gt;&lt;td ← 它应该是 &lt;/font&gt;&lt;/td&gt;', '&lt;td - 我在上面修复了(不必...因为它被正确解析了)。所以,是的,首先确保你得到一个格式正确的HTML array 至少。

正是因为这样的事情,用正则表达式解析 HTML 是个坏主意。即使有上面​​的错误——HTML 被正确解析了sh——但是提取内容,严格使用正则表达式,会导致它绝对失败。


对 Node 使用 jsdom - 您的代码应该如下所示:

const jsdom = require("jsdom");
const { JSDOM } = jsdom;

const td = ['<td bgcolor="#2D2F34">&nbsp;<font color="#999999">Name: </font><a href="site.php?page=send&sendto=Username"><font color="#999999">Username</font></a>&nbsp;&nbsp;&nbsp;</td>', '<td bgcolor="#2D2F34">&nbsp;<font color="#999999">Crew: </font><a href="site.php?page=crewprofile&id=2120"><font color="#999999">My Crew</font></a> </td>', '<td bgcolor="#2D2F34">&nbsp;<font color="#999999">Wealth: Rich</font></td>', '<td bgcolor="#2D2F34">&nbsp;<font color="#999999">Rank: Hitman</td>', '<td bgcolor="#2D2F34">&nbsp;<font color="#999999">Status: Alive ( </font><font color=green>Online</font><font color="#999999"> )</font></td>', '<td bgcolor="#2D2F34">&nbsp;<font color="#999999">Messages sent: 3</font></td>', '<td bgcolor="#2D2F34">&nbsp;<font color="#999999">Messages received: 1</font></td>'];

const dom = new JSDOM(`<table><tr>${td.join("")}</tr></table>`);
const frag = dom.window.document;

const data = [...frag.querySelectorAll("td")].reduce((ob, td) => {
    const a = td.textContent.split(':');
    ob[a[0].trim()] = a.slice(1).join(":").trim();
    return ob;
}, {});

console.log( data );

【讨论】:

  • 谢谢。是的,我需要整理出我正在获取的内容。我已经安装了 jsdom 并将尝试一下,如果一切都失败了,我将使用您上面提供的代码。使用 jsdom 会更简单吗?如果可能的话,你能举个例子吗?谢谢!
  • @JanHenning 添加了一个未经测试的示例,但只需查看他们的文档即可。
  • 谢谢,不胜感激!
  • @JanHenning 找了一分钟来测试。如果您无法正常工作,请更新我的答案。
  • 谢谢!!我要看看我能不能整理我的取件。看看我是否也可以从只使用正则表达式转向 jsdom。
猜你喜欢
  • 2023-02-09
  • 2021-07-22
  • 1970-01-01
  • 2023-03-18
  • 2018-05-29
  • 2015-02-13
  • 2013-03-08
  • 1970-01-01
  • 2015-08-14
相关资源
最近更新 更多