7

我必须为我的 iOS 应用程序解析一个 Html。我在网上看到我应该使用 Xpath,然后找到了库 TFHpple。我看到这个库使用 XpathQuery

<!DOCTYPE "html">
<html>   
    <head>
                <meta property="og:site_name" content="Sito 4"/>
        <meta property="og:title" content="home"/>
        <meta name="viewport" content="width=320" />
        <meta name="keywords" content="mobile website,microsite, mobdis,iphone,android" />
        <meta name="description" content="Amazingly designed using MobDis.com" />
        <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
        <meta name="format-detection" content="telephone=no" />
        <title>Sito 4/home</title>

        <!--[if (!IE)|(gte IE 8)]><!-->
<link href="http://cdn2.mobdis.me/assets/publish-3.4-datauri.css" media="screen" rel="stylesheet" type="text/css" />
<!--<![endif]-->
<!--[if lte IE 7]>
<link href="http://cdn2.mobdis.me/assets/publish-3.4.css" media="screen" rel="stylesheet" type="text/css" />
<![endif]-->


        <script type="text/javascript"></script>
        <style type="text/css">
            body{
                background-color: black;
            }
        </style>

    </head>
    <body>


        <div id='processingScreen' style="width:320px;height:417px;background-color:rgb(0, 0, 128)"><img alt="Publish_loading" src="http://cdn2.mobdis.me/pro_images/publish_loading.gif" style="max-width:100%;max-height:100%;" />
        </div>

        <div data-role="page" id="home" data-id="94568">


            <div data-role="content" id="area" class="canvas" style="width:320px;height:417px;background-color:rgb(0, 0, 128)">





            <div id="text-1" data-name="text-1" class="drsElement drsMoveHandle unselectable parentDef" data-content="text" style="width: 320px; height: 50px; top: 0px; left: 0px; z-index: 1; overflow: visible; border-top-left-radius: 5px; border-top-right-radius: 5px; border-bottom-right-radius: 5px; border-bottom-left-radius: 5px; -webkit-box-shadow: rgb(255, 255, 255) 0px 2px 0px inset; "><div style="width: 320px; height: 50px; background-color: transparent; border-color: rgb(112, 150, 171); border-width: 2px; border-style: none none solid; border-top-left-radius: 5px; border-top-right-radius: 5px; border-bottom-right-radius: 5px; border-bottom-left-radius: 5px; background-image: -webkit-gradient(linear, 0% 0%, 0% 100%, from(rgb(181, 221, 242)), to(rgb(181, 221, 242))); " class="txtDef childDef editable" data-pageid="" data-pageurl="" data-exlink="" data-transition="0" id="text-1_content" title="text-1" data-allborderradius="unchecked"><div>
    &nbsp;</div>
<div>
    <span style="color:#ffffff;"><span style="font-family: futura; "><span style="font-size: 26px; "><span style="text-shadow: rgba(0, 0, 0, 0.59375) 1px 1px 2px; "><strong>Sito 4</strong></span></span></span></span></div>
</div></div><div id="image-1" data-name="image-1" class="drsElement drsMoveHandle unselectable parentDef" data-content="image" style="width: 320px; height: 169px; top: 48px; left: 0px; z-index: 2; " data-src="assets/promozioni.png"><img src="http://cdn2.mobdis.me/uploads/73005/original.png?1375193775" style="width: 320px; height: 169px; " data-pageid="" data-pageurl="" data-exlink="" data-transition="0" id="image-1_content" title="image-1" class="childDef"></div><div id="text-2" data-name="text-2" class="drsElement drsMoveHandle unselectable parentDef" data-content="text" style="width: 320px; height: 100px; top: 277px; left: 0px; z-index: 3; overflow: visible; "><div style="width: 320px; height: 100px; " class="txtDef childDef editable" data-pageid="" data-pageurl="" data-exlink="" data-transition="0" id="text-2_content" title="text-2"><div>
    <span style="color:#ffffff;"><span style="font-size: 20px; "><strong>Complimenti hai ottenuto una fantastica promozione!</strong></span></span></div>
</div></div>
            <div id="text-3" data-name="text-3" class="drsElement drsMoveHandle unselectable parentDef" data-content="text" style="width: 320px; height: 65px; top: 332px; left: 0px; z-index: 4; overflow: visible; "><div style="width: 320px; height: 65px; " class="txtDef childDef editable" data-pageid="" data-pageurl="" data-exlink="" data-transition="0" id="text-3_content" title="text-3"><div style="text-align: left; ">
    <span style="font-size:12px;"><span style="color: rgb(255, 255, 255); ">Scadenza: 29 Ago 2013</span></span></div>
</div></div>
            </div>


        </div>








            <div id="divAds"><a href="http://www.mobdis.com" target="_blank"><img alt="Banner" src="http://cdn2.mobdis.me/pro_images/banner.png" style="display:inline;max-width:100%;" /></a></div>





                <div id="divBrowserAlert">The current version of your browser is not supported yet. To ensure full functionality, we recommend using Apple Safari or Google Chrome.<br/>
                    <button onclick="dismissUnsupportedBrowser('')">Dismiss</button>

                    <!--<button onclick=window.location = "";">Redirect</button>-->

                </div>

                <script type="text/javascript">

                </script>




        <script type="text/javascript">
            var mpq = [];
            mpq.push(["init", '5fed56e1e4a85990c0fb6ff5294d2d42']);
            (function(){var b,a,e,d,c;b=document.createElement("script");b.type="text/javascript";b.async=true;b.src=(document.location.protocol==="https:"?"https:":"http:")+"//api.mixpanel.com/site_media/js/api/mixpanel.js";a=document.getElementsByTagName("script")[0];a.parentNode.insertBefore(b,a);e=function(f){return function(){mpq.push([f].concat(Array.prototype.slice.call(arguments,0)))}};d=["init","track","track_links","track_forms","register","register_once","identify","name_tag","set_config"];for(c=0;c<d.length;c++){mpq[d[c]]=e(d[c])}})();
            mpq.track("project_id-13003");
            mpq.name_tag('mobdis');
        </script>

                <div id="scriptImports">
            <script src="http://cdn2.mobdis.me/assets/jq-3.4.js" type="text/javascript"></script>

            <script src="http://cdn2.mobdis.me/assets/publish-3.4.js" type="text/javascript"></script>
            <script src="http://cdn2.mobdis.me/assets/jqm-3.4.js" type="text/javascript"></script>






<!--   -->

            <script type="text/javascript">
                var root = "http://sito4.mobdis.co/";
                var cdn_link = "http://cdn2.mobdis.me/";

                var require_passcode = false;

                var showWaterMark = false;
                var showNone = false;


            </script>

        </div>

        <div id="divAutho" class="autho_56930"></div>


        <div id="restrictMode" class="divBasic"></div>
        <noscript style="color:red;font-size:1em;top:0px;left:0px;position:absolute;display:block;background: #363636;color:#fbb041;height:100%;text-align:center;">Javascript is disabled in your browser.<br /> Please enable the javascript.</noscript>
    </body>
</html>

我对有关此 html 的两个信息很感兴趣:

  • 这个<title>Sito 4/home</title>

  • 这个<span style="color: rgb(255, 255, 255); ">Scadenza: 29 Ago 2013</span>

现在我写了这段代码:

- (void)loadDataFromHtml {
    NSURL *url = [NSURL URLWithString:stringUrl];
    NSData *data = [NSData dataWithContentsOfURL:url];

    TFHpple *parser = [TFHpple hppleWithHTMLData:data];

    NSString *XpathQueryStringTitle = @"//title";
    NSArray *nodes = [parser searchWithXPathQuery:XpathQueryStringTitle];

    NSMutableArray *dataArray = [[NSMutableArray alloc]initWithCapacity:0];
    for (TFHppleElement *element in nodes) {
        HtmlData *htmlData = [[HtmlData alloc]init];
        [dataArray addObject:htmlData];
        htmlData.title = [[element firstChild]content];
        htmlTitle = htmlData.title;
        htmlTitle = [htmlTitle substringToIndex:6];
        htmlTitle = [htmlTitle stringByReplacingOccurrencesOfString:@" " withString:@""];
    }
}

(基于本教程:http ://www.raywenderlich.com/14172/how-to-parse-html-on-ios )

现在我的问题是如何找到我需要的第二个信息?我应该制作另一个 NSString 来获取第二个信息吗?你能帮我解决这个问题吗?谢谢

4

3 回答 3

7

我通过使用 Firebug 解决了我的问题。我用 Firefox 打开了我的页面,然后打开了 Firebug,我选择了我需要的信息所在的行,然后我用鼠标右键单击它,然后选择“复制 Xpath”,然后将其粘贴到我的应用程序中它正在工作!

谢谢你的建议!

于 2013-07-31T09:45:59.990 回答
4

检查这个:

https://github.com/mwaterfall/MWFeedParser

https://github.com/zootreeves/Objective-C-HMTL-Parser

这将为 iphone sdk 提供 HTML 解析器。

更多帮助:

于 2013-07-31T09:09:52.107 回答
1

我已经成功地使用了Nolan Waite 的 HTMLReader项目来轻松可靠地解析一个非常复杂的 HTML5 响应。

它易于设置和使用,我强烈推荐它。我发现的所有其他项目都是旧的、未维护的或旨在解析严格的XHTML / XML。

这是自述文件中的示例:

#import <HTMLReader/HTMLReader.h>

// Parse a string and find an element.
NSString *markup = @"<p><b>Ahoy there sailor!</b></p>";
HTMLDocument *document = [HTMLDocument documentWithString:markup];
NSLog(@"%@", [document firstNodeMatchingSelector:@"b"].textContent);
// => Ahoy there sailor!

// Wrap one element in another.
HTMLElement *b = [document firstNodeMatchingSelector:@"b"];
NSMutableOrderedSet *children = [b.parentNode mutableChildren];
HTMLElement *wrapper = [[HTMLElement alloc] initWithTagName:@"div"
                                                 attributes:@{@"class": @"special"}];
[children insertObject:wrapper atIndex:[children indexOfObject:b]];
b.parentNode = wrapper;
NSLog(@"%@", [document.rootElement serializedFragment]);
// => <html><head></head><body><p><div class="special"> \
<b>Ahoy there sailor!</b></div></p></body></html>

// Load a web page.
NSURL *URL = [NSURL URLWithString:@"https://github.com/nolanw/HTMLReader"];
NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithURL:URL completionHandler:
  ^(NSData *data, NSURLResponse *response, NSError *error) {
      NSString *contentType = nil;
      if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
          NSDictionary *headers = [(NSHTTPURLResponse *)response allHeaderFields];
          contentType = headers[@"Content-Type"];
      }
      HTMLDocument *home = [HTMLDocument documentWithData:data
                                        contentTypeHeader:contentType];
      HTMLElement *div = [home firstNodeMatchingSelector:@".repository-description"];
      NSCharacterSet *whitespace = [NSCharacterSet whitespaceAndNewlineCharacterSet];
      NSLog(@"%@", [div.textContent stringByTrimmingCharactersInSet:whitespace]);
      // => A WHATWG-compliant HTML parser in Objective-C.
  }] resume];
于 2015-11-07T20:49:22.223 回答