TFHpple使用方法
来源:互联网 发布:php代码批量替换工具 编辑:程序博客网 时间:2024/06/06 17:29
TFHpple是OC解析HTML第三方库。GitHub地址:https://github.com/topfunky/hpple/tree/master
使用前需要:
1.添加libxml2.2.dylib
2.设置Header Search Paths: (1)debug为/usr/include/libxml2; (2)release为${SDKROOT}/usr/include/libxml2
#import "TFHpple.h"
NSData * data = [NSData dataWithContentsOfFile:@"index.html"];
TFHpple * doc = [[TFHpple alloc] initWithHTMLData:data];
NSArray * elements = [doc search:@"//a[@class='sponsor']"];
TFHppleElement * element = [elements objectAtIndex:0];
[e text]; // The text inside the HTML element (the content of the first text node)
[e tagName]; // "a"
[e attributes]; // NSDictionary of href, class, id, etc.
[e objectForKey:@"href"]; // Easy access to single attribute
[e firstChildWithTagName:@"b"]; // The first "b" child node
NSData * data = [NSData dataWithContentsOfFile:@"index.html"];
TFHpple * doc = [[TFHpple alloc] initWithHTMLData:data];
NSArray * elements = [doc search:@"//a[@class='sponsor']"];
TFHppleElement * element = [elements objectAtIndex:0];
[e text]; // The text inside the HTML element (the content of the first text node)
[e tagName]; // "a"
[e attributes]; // NSDictionary of href, class, id, etc.
[e objectForKey:@"href"]; // Easy access to single attribute
[e firstChildWithTagName:@"b"]; // The first "b" child node
使用Swift解析实例
示例URL:http://computer.iscnu.net/Welcome/Login,用Chrome显示源代码。
重点是xpathQueryString的写法,我代码中let xpathQueryString = "//p[@class='cl']/label/a[@href='/Welcome/Register.html']" ,下面一一道清其写法。
//p 表示要找的节点名称是p
[@class='cl'] 可省去,加上则表示要找的是"class"这个属性是"cl"的p节点
/label 表示找p节点下名为label的子节点
println(firstMatching)后可以看到:
可见,一个节点有nodeAttributeArray、nodeChildArray和nodeName三部分组成。nodeAttributeArray由attributeName和nodeName组成;nodeChildArray由nodeContent和nodeName组成,nodeChildArray中可以再嵌入nodeChildArray;最后的nodeName就是要找的节点名称,注意,我们可能会看到多个nodeName,要注意区分。
将 xpathQueryString ="//p[@class='cl']" ,然后println(firstMatching),可看到复杂一点的结果:
对应的HTML:
分析:(这样看应该清楚一点)
nodeAttributeArray = (
{ attributeName = class; nodeContent = cl; }
);
nodeChildArray = (
{ nodeContent = "\n\t\t\t\t"; nodeName = text; }
{ nodeAttributeArray =(
{ attributeName = class; nodeContent = username; },
{ attributeName = tabindex; nodeContent = 1; },
{ attributeName = type; nodeContent = text; },
{ attributeName = name; nodeContent = account; },
{ attributeName = value; nodeContent = ""; },
{ attributeName = placeholder; nodeContent = ""; }
);
nodeName = input;
}
{ nodeContent = "\n\t\t\t\t"; nodeName = text; }
{ nodeChildArray = (
{ nodeAttributeArray = (
{ attributeName = href; nodeContent = "/Welcome/Register.html"; }
);
nodeChildArray = (
{ nodeContent = "\U7528\U6237\U6ce8\U518c"; nodeName = text; },
);
nodeName = a;
}
);
nodeName = label;
}
);
nodeName = p;
有几点规律:
1.有nodeAttributeArray,就一定有nodeName;
2.在nodeAttributeArray下,有AttributeName和nodeContent;
3.在nodeAttributeArray中可以插入nodeChildArray;
4.在nodeChildArray下有nodeContent和nodeName(这个nodeName是子节点的名称),可以再嵌入nodeAttributArray
5.最简单的nodeChildArray是换行退格,其nodeName是text。
0 0
- TFHpple使用方法
- ASIHTTPRequest+TFHpple
- TFHpple 框架解析HTML
- 使用TFHpple解析html
- 使用TFHpple解析html
- 使用TFHpple解析html
- 解析html文件,tfhpple工具
- iOS开发-解析Html-TFHpple
- HTML解析之TFHpple 包的使用
- 万方数据知识平台 TFHpple +Xpath解析
- 使用TFHpple库解析HTML和XML
- IOS开发中使用TFHpple解析html
- 编写小程序查看CSDN博客信息(ASIHTTPRequest+TFHpple)
- iOS使用TFHpple解析HTML遇到的一些问题
- 使用TFHpple解析GB2312网页出现乱码的解决方法
- 使用TFHpple第三方库解析html简介
- 使用方法
- iOS 用TFHpple抓取GB-2312编码的html页面,页面返回编码错误
- 图像处理
- Android 保存View到本地SD卡
- 2016年 成渝校招软件上机练习
- Palindrome Number
- 操作系统过小,无法启动数据库
- TFHpple使用方法
- Google笔试(2015年8月)
- UVa 340 Master-Mind Hints
- 1090. Highest Price in Supply Chain (25)
- Java基础 笔记(2)
- hibernate一对一,一对多,多对一,多对多配置
- UVA 193 Graph Coloring
- Android 开源框架Universal-Image-Loader完全解析(三)
- Android ADB工具-管理设备 app(二)