1

如何在 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>

问题是如何在一次运行中获取所有A标签。class="hubs"我怎么能这样做?

4

1 回答 1

1

您可以使用//搜索语法搜索 HTML 中的任何位置,然后使用该@class='xxx'语法搜索特定类的标签。因此,您可以搜索//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);
}

请参阅Ray Wenderlich 网站上的如何在 iOS 上解析 HTML 。

于 2014-12-21T18:13:50.170 回答