【问题标题】:Jsoup split block element to preserve order of text and child elementsJsoup拆分块元素以保留文本和子元素的顺序
【发布时间】:2021-04-13 04:11:49
【问题描述】:

假设我有一个这样的 html 片段:

<p>Generally speaking, in the U.S., if you want to <a href="\&quot;https://web.archive.org/web/20210408195204/https://www.wsj.com/articles/investors-big-and-small-are-driving-stock-gains-with-borrowed-money-11617799940\&quot;"> 
  borrow money from your broker to buy stocks</a>, you are capped at 2-to-1 leverage. If you have $100, you can buy $200 worth of stock. Back in the olden days, you could have bought&nbsp;$300 or $500 or $1,000 of stock with your $100, borrowing the rest from your broker, but then a Great Depression happened and regulators clamped down on margin lending. </p>

我已经用jsoup 对其进行了解析,并将其表示为Element。

我希望能够将元素拆分为:

  • 一段文字:"Generally speaking, in the U.S., if you want to "
  • 一个元素&lt;a href="\&amp;quot;https://web.archive.org/web/20210408195204/https://www.wsj.com/articles/investors-big-and-small-are-driving-stock-gains-with-borrowed-money-11617799940\&amp;quot;"&gt;
  • 另一段文字:"borrow money from your broker to buy stocks&lt;/a&gt;, you are capped at 2-to-1 leverage. If you have $100, you can buy $200 worth of stock. Back in the olden days, you could have bought&amp;nbsp;$300 or $500 or $1,000 of stock with your $100, borrowing the rest from your broker, but then a Great Depression happened and regulators clamped down on margin lending"

并在保持这些部分的顺序的同时做到这一点。

到目前为止我看过的东西:

  • getAllElements() 仅返回索引 0 处的 p 标签本身,然后返回 a 标签的元素
  • children() 只为 a 标签返回 1 个元素。

【问题讨论】:

    标签: java jsoup


    【解决方案1】:

    你想要的是childNodes() 方法。然后你会得到一个List&lt;Node&gt;。您可以使用这些节点,访问它们的属性和父节点,但是您无法使用 select() 访问更深的标签,因为只有 Element 类有此方法。

    【讨论】:

      猜你喜欢
      • 2014-07-22
      • 1970-01-01
      • 2013-07-13
      • 2019-08-13
      • 1970-01-01
      • 2010-11-23
      • 1970-01-01
      • 1970-01-01
      • 2016-08-13
      相关资源
      最近更新 更多