happle的使用

hpple在网页解析方面有很重要的作用,本次项目中用到合作单位提供的一个信号标识,但是对方不提供接口只提供HTML网页解析,所以客户端只能拿到HTML源码之后对HTML源码进行解析,拿到自己需要的数据。

demo地址:https://github.com/misszero8090/httpParse

  • 先下载hpple库,推荐使用cocoapods导入,但因我们项目原因,这里重点说后者,手动导入。去github下载包,地址:https://github.com/topfunky/hpple

  • 将库引入project , 添加静态库libxml2.2.dylib,注意我使用的是xcode8.3.3 iOS10开发,默认情况下是.tbd, 因为库需要用.dylib,所以在/usr/lib目录下还有.dylib文件。

  • header search path设置$(SDKROOT)/usr/lib/libxml2

  • user Heather search path设置$(SDKROOT)/usr/include/libxml2

  • FrameWork search path设置/usr/lib/libxml2.2.dylib

  • 以上设置完成之后,导入#import "TFHpple.h"

Objective-C解析HTML<后附 RSSI.html源代码>
拿到rssi后面的值:

NSString *htmlPath = [[NSBundle mainBundle] pathForResource:@"RSSI" ofType:@"html"];
    NSData *htmlData = [NSData dataWithContentsOfFile:htmlPath];

    TFHpple *doc = [[TFHpple alloc] initWithHTMLData:htmlData];
    NSArray *elements = [doc searchWithXPathQuery:@"//fieldset[@class='cbi-section']"];

    for (TFHppleElement *elment in elements) {
    for (TFHppleElement *el in [elment children]) {

        NSArray *els =  [el searchWithXPathQuery:@"//table"];
        for (TFHppleElement *e in els) {
            for (TFHppleElement *table in [e children]) {

                NSArray *tables = [table children];
                for (TFHppleElement *tr in tables) {

                    if ([[tr text] isEqualToString:@"RSSI"]) {
                        NSUInteger index = [tables indexOfObject:tr];
                        NSLog(@"text:%@",[tr text]);
                        NSLog(@"index:%ld",index);

                        TFHppleElement *rssi = tables[index + 1];

                        NSLog(@"rssi:%@",[rssi text]);
                    }  
                }
            }
        }    
    }
}

//方法2:硬拿 <知道元素的位置,且网页结构不会发生改变>
TFHppleElement *element = elements[2];
NSArray *els = [element children];
TFHppleElement *el = els[3];
NSArray *es = [el children];
TFHppleElement *e = es[9];
NSArray *ls = [e children];
TFHppleElement *l = ls.lastObject;
 NSLog(@"html:%@",[l text]);

附:RSSI.HTML文件












NHRT - 总览 - LuCI







状态

系统
主机名NHRT
主机型号NHPT CR5400
固件版本 Cobra P015C005B025
内核版本3.18.29
本地时间-
运行时间-
平均负载-
内存
可用数-
空闲数-
已缓冲-
4G网络
固件版本10.21.3.14.xw_trk_hk_00.05
运营商46000
注册状态2,1,11b2,0003,7
小区信息PLMN ID:46000,cell ID:418,mode:17,band:38,channel:37900,bandwidth:6,TDD UL/DL configuration:2,RSSI:50,RSRP:34 OK
RSSI-104 dBm
IMSI460025111043733
IP10.60.114.171
专有承载数0
网络
IPv4 WAN状态
![](/luci-static/resources/icons/ethernet_disabled.png)
?
收集数据:
IPv6 WAN状态
![](/luci-static/resources/icons/ethernet_disabled.png)
?
收集数据:
活动连接-
DHCP分配
主机名 IPv4-地址 MAC-地址 剩余租期

收集数据:
无线
收集数据:
已连接站点
MAC-地址 网络 信号 噪声 接收速率 发送速率

收集数据:

>>>>> Cobra P015C005B025 >>>>>

你可能感兴趣的:(happle的使用)