【问题标题】:Hpple with complex HTML in iOS在 iOS 中处理复杂的 HTML
【发布时间】:2015-02-19 20:26:40
【问题描述】:

如何在 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 标签。我该怎么做?

【问题讨论】:

  • 我的问题是一次运行就可以在 class="hubs" 中获取所有 A 标签。我该怎么做?

标签: html ios parsing hpple


【解决方案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 网站上的 How to Parse HTML on iOS

【讨论】:

    猜你喜欢
    • 2013-01-04
    • 2020-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-20
    • 2019-12-31
    • 1970-01-01
    • 2021-07-07
    相关资源
    最近更新 更多