hpple使用几个注意点
2014-01-13 20:35
387 查看
hpple功能有限,使用的时候要注意一个原则:如果页面复杂,节点较多,解析的html需要先截取,xpath路径尽可能短,使用简单语法。
XPath语法不再赘述,本文的话题是分析得到的一个TFHppleElement数组。
对于数组里的某个TFHppleElement可能有如下结构:
一个完备的节点一般包括属性数组,子节点数组,节点名三部分。一个节点的类型由节点名字决定。像上面就包含了div,a和text三种节点。任一节点是字典类型的。
假设我们令elem为指向上述结构的指针,则
[elem children];得到子节点数组
[elem childrenWithTagName:@"text"];可以得到div节点的所有文本子节点。
[elem firstChildrenWithTagName:@"text"];则可以得到div节点的第一个文本节点。
[elem tagName]; 则得到@"div"
[elem attributes];可以得到div的属性字典,显然这里没
这里我们引入指向子节点a的指针a,则
[a objectForKey:@“href”];得到@"/character/ch0146096/?ref_=ttfc_fc_cl_t1"
[a firstTextChild]content]; 得到@“"Tyrion Lannister"
[a text];此处和上句效果一致,返回第一个文本节点的内容
但是[elem text]为@"\n \U00a0"
本例中我们没用到- (NSArray*) childrenWithClassName:(NSString*)className和- (TFHppleElement *) firstChildWithClassName:(NSString*)className
显然class和tagName是地位等同的,对于如下情况,我们就可以使用上述方法
设elem指向body节点,[elem childrenWithClassName:@"intro"]打印出来就是:
补充一点,对于a的某个属性节点,结构为
一个属性节点没有属性数组,只有文本子节点数组和节点名。
XPath语法不再赘述,本文的话题是分析得到的一个TFHppleElement数组。
对于数组里的某个TFHppleElement可能有如下结构:
{ nodeChildArray = ( { nodeContent = "\n \U00a0"; nodeName = text; }, { nodeAttributeArray = ( { attributeName = href; nodeContent = "/character/ch0146096/?ref_=ttfc_fc_cl_t1"; } ); nodeChildArray = ( { nodeContent = "Tyrion Lannister"; nodeName = text; } ); nodeName = a; }, { nodeContent = "\n (28 episodes, 2011-2013)\n "; nodeName = text; } ); nodeName = div; }
一个完备的节点一般包括属性数组,子节点数组,节点名三部分。一个节点的类型由节点名字决定。像上面就包含了div,a和text三种节点。任一节点是字典类型的。
假设我们令elem为指向上述结构的指针,则
[elem children];得到子节点数组
[elem childrenWithTagName:@"text"];可以得到div节点的所有文本子节点。
[elem firstChildrenWithTagName:@"text"];则可以得到div节点的第一个文本节点。
[elem tagName]; 则得到@"div"
[elem attributes];可以得到div的属性字典,显然这里没
这里我们引入指向子节点a的指针a,则
[a objectForKey:@“href”];得到@"/character/ch0146096/?ref_=ttfc_fc_cl_t1"
[a firstTextChild]content]; 得到@“"Tyrion Lannister"
[a text];此处和上句效果一致,返回第一个文本节点的内容
但是[elem text]为@"\n \U00a0"
本例中我们没用到- (NSArray*) childrenWithClassName:(NSString*)className和- (TFHppleElement *) firstChildWithClassName:(NSString*)className
显然class和tagName是地位等同的,对于如下情况,我们就可以使用上述方法
<html> <head> <style type="text/css"> h1.intro {color:blue;} p.important {color:green;} </style> </head> <body> <h1 class="intro">Header 1</h1> <p>A paragraph.</p> <p class="important">Note that this is an important paragraph.</p> </body> </html>
设elem指向body节点,[elem childrenWithClassName:@"intro"]打印出来就是:
elem is ( "{\n nodeAttributeArray = (\n {\n attributeName = class;\n nodeContent = intro;\n }\n ); \n nodeChildArray = (\n {\n nodeContent = \"Header 1\";\n nodeName = text;\n }\n ); \n nodeName = h1;\n}" )
补充一点,对于a的某个属性节点,结构为
{ nodeChildArray = ( { nodeContent = "/character/ch0146096/?ref_=ttfc_fc_cl_t1"; nodeName = text; } ); nodeName = href; }
一个属性节点没有属性数组,只有文本子节点数组和节点名。
相关文章推荐
- 机器学习&数据挖掘笔记_25(PGM练习九:HMM用于分类)
- 游戏开发随手记:cocos2d ccLayer响应触摸事件方法
- 套用今天的签名“最新的VS+最新的MATLAB+最新的WINDOWS=无尽的烦恼 ”,用了一天的时间去配置相关的环境,确实很囧。好在现在问题解决了,特发布过程如下,希望能给有需要的朋友带来帮助。
- 远程无法访问mysql Can't connect to MySQL server on "host" (111)
- 【leetcode】 Single Number
- 动态加载的 CTreeCtrl 消息响应
- 浏览器助手,请求拦截,后台模拟键鼠操作,页内嵌入JS
- daemontools管理fast-fail的zookeeper
- SPSS做单因素方差分析
- iOS开发-模式视图
- 上传附件<input type="file">规则
- 《数字图像处理》学习笔记(八)--全局阈值分割
- 对Java开发的思考(代码组织、设计模式、开发流程)
- iOS 6会出现 UItextfield 无法输入任何字符的 bug
- 南邮-1181-lucky
- 在线支付
- python3自动下载优酷视频小程序
- zkClient的并发控制
- LaTeX命令速查
- 电脑进程