【问题标题】:Parsing HTML into JSON将 HTML 解析为 JSON
【发布时间】:2010-12-31 16:57:53
【问题描述】:

我的任务是获取所有 SMS updates from this page 并使用 Yahoo Pipes 将它们放入 JSON 提要。我不完全确定如何获得每次更新,因为它们不是单独的元素,而只是标题的集合等。任何共享的智慧将​​不胜感激!

【问题讨论】:

    标签: html xml json dom yahoo-pipes


    【解决方案1】:
    <h1 id="blogtitle">SMS Update</h1> 
    <div class="blogposttime blogdetail">Left at 2nd January 2010 at 01:12</div> 
    <div class="blogcategories blogdetail">Recieved by SMS (Location: Pokhara - Nepal)</div> 
    <p class="blogpostmessage"> 
    RACE DAY! We took the extra day off to pimp the rick some more, including a huge Australian flag. Quiet night at a pub with 6 other teams. Time for brekkie and then we're off to the rickshaw grounds for 8:30 for 10am start.
    </p> 
    

    对于 DOM/XML 解析器来说,这似乎是一项相当容易的工作。

    由于块没有包含在 XML 标记中,您可以查找每个块中存在的元素,例如 &lt;h1 id="blogtitle"&gt;SMS Update&lt;/h1&gt; 定义了新块的开始。

    使用您的 DOM 解析器查找 ID 为 blogtitle 的所有元素。此时,您可以使用 DOM 函数来引用 blogtitle 元素的 nextSibling。您只需要 blogtitle 元素之后的 3 个兄弟姐妹。

    通过一些工作,您可以轻松地使用此逻辑来构建您的 JSON 对象。

    【讨论】:

    • 谢谢,但我通过查找页面上的所有 blogtitle 元素以及 posttime 等来“解决”这个问题,并使用 Nokogiri (Ruby) 对其进行迭代,因为它们总是在正确的顺序。似乎在游泳。不过,谢谢您的回答。
    猜你喜欢
    • 2021-12-26
    • 2020-06-14
    • 2013-08-18
    • 1970-01-01
    • 1970-01-01
    • 2014-05-19
    • 2013-11-15
    • 1970-01-01
    相关资源
    最近更新 更多