2010-01-02 57 views
1

我的任务是获取所有SMS updates from this page并将它们放入使用Yahoo Pipes的JSON供稿中。我不完全确定我将如何获得每一次更新,因为它们不是单独的元素,而只是标题的集合等。任何共同的智慧都将非常感谢!将HTML解析为JSON

回答

2
<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标签中,因此可以查找每个块中存在的元素,例如<h1 id="blogtitle">SMS Update</h1>定义了新块的开始。

使用您的DOM解析器查找所有编号为的博客文章。此时,您可以使用DOM功能来引用博客文章元素的nextSibling。所有你需要的是博客标题元素之后的兄弟姐妹。

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

+0

谢谢,但我通过查找页面上的所有blogtitle元素以及posttime等来“解决”这个问题,并且使用Nokogiri(Ruby)迭代它们,因为它们总是处于正确的顺序。似乎正在游泳。不过,谢谢你的回答。 – 2010-01-02 07:47:34