html - Hpple 在 iOS 中使用复杂的 HTML

标签 html ios parsing hpple

我如何在 iOS 中使用 hpple 解析包含许多子标签的复杂 html。我找不到关于这个库的任何好的和复杂的教程或任何好的文档。通过简单的解析没有任何问题。 html 示例是:

<div class="post shortcuts_item focus" id="post_243273">
<div class="published">today in 21:03</div>
<h1 class="title">
    <a href="http://example.ru/post/243273/" class="post_title">Some simple title text here</a>
</h1>

<div class="hubs">
    <a href="http://example.ru/hub/internet_regulation/" class="hub">Simple text 1</a><span class="profiled_hub" title="Profile 1">*</span>, 
    <a href="http://example.ru/hub/business-laws/" class="hub">Simple text 2</a><span class="profiled_hub" title="Profile 2">*</span>, 
    <a href="http://example.ru/hub/vkontakte/" class="hub">Simple text 3</a><span class="profiled_hub" title="Profile 3">*</span>, 
    <a href="http://example.ru/hub/social_networks/" class="hub">Simple text 4</a><span class="profiled_hub" title="Profile 4">*</span>, 
    <a href="http://example.ru/hub/facebook/" class="hub">Simple text 5</a><span class="profiled_hub" title="Profile 5">*</span>
</div>
<div class="content html_format">
    <img src="//example.org/files/aa9/f6c/8a0/aa9f6c8a049e405c9f72bf10ee3a2e9f.png"><br>
    <br>
    Simple but very long text with link tag  <a href="http://example.ru/post/243273/#blahblah">Simple link...</a>
    <div class="buttons">
        <a href="http://example.ru/post/243273/#habracut">Read more...</a>
    </div>
    <div class="clear"></div>   
</div>
</div>

问题是如何一次性获取 class="hubs" 内的所有 A 标签。我该怎么做?

最佳答案

您可以使用 // 搜索语法在 HTML 中的任何位置搜索,然后使用 @class='xxx' 语法搜索特定的标签类(class)。因此,您可以搜索 //a[@class='hub']//div[@class='hubs']/a:

TFHpple *parser = [TFHpple hppleWithHTMLData:data];
NSArray *nodes = [parser searchWithXPathQuery:@"//a[@class='hub']"];
for (TFHppleElement *element in nodes) {
    NSString *href = [element attributes][@"href"];
    NSString *content = [element content];
    NSLog(@"%@ -> %@", href, content);
}

参见 How to Parse HTML on iOS在 Ray Wenderlich 的网站上。

关于html - Hpple 在 iOS 中使用复杂的 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27590822/

相关文章:

html - CSS - DIV 对齐问题

javascript - 为什么以下代码中的警报框不断触发?

ios - 我有一个使用 XIB 文件创建的自定义 UICollectionViewCell。如何让此单元在点击时将另一个 Controller 插入堆栈

parsing - Rust 编译器如何标记泛型中的 '>' 与 '>>'?

javascript - 无法使用 requirejs 加载插件 jquery-visible

ios - 我们可以在 WKImageView 中使用 GIF 吗?

ios - 在用户点击 Google Maps iOS 应用程序的任何地方添加标记?

php - 解析包含分号的 SQL(在 PHP 中)

parsing - 在 golang 中解析 NeDB 文件

javascript - 视差,作为主页横幅