我在使用 TFHpple 时遇到了一些问题,所以这里是:我想解析以下几行:
<div class=\"head\" style=\"height: 69.89px; line-height: 69.89px;\">
<div class=\"cell editable\" style=\"width: 135px;\"contenteditable=\"true\">
<p> 1</p>
</div>
<div class=\"cell\" style=\"width: 135px;\" contenteditable=\"false\">
<p>2</p>
</div>
</div>
<div style=\"height: 69.89px; line-height: 69.89px;\" class=\"head\">
<div class=\"cell\" style=\"width: 135px; text-align: left;\"contenteditable=\"false\">
<p>3 </p>
</div>
<div class=\"cell\" style=\"width: 135px;\" contenteditable=\"false\">
<p>4</p>
</div>
</div>
<div style=\"height: 69.89px; line-height: 69.89px;\" class=\"\">
<div class=\"cell\" style=\"width: 135px;\" contenteditable=\"false\">
<p>5</p>
</div>
<div class=\"cell\" style=\"width: 135px;\" contenteditable=\"false\">
<p>6</p>
</div>
</div>
现在我想将第一级div
“元素”(对不起,我不知道正确的术语)放在一个数组中。因此,我尝试通过简单地/div
将 xPath 提供给searchWithXPathQuery
方法来做到这一点,但它根本找不到任何东西。
我的第二个解决方案是尝试使用这种路径://div[@class=\"head\"]
但也允许[@class=\"\"]
但我什至不知道这是否可能。(我想这样做是因为我需要元素在数组中的顺序与它们在数据中的顺序相同)
所以这是我的问题,TFHpple 不能使用有什么特别的原因/div
吗?如果没有办法只取第一级div
,那么是否可以使用 xPath(这里是属性class
)对属性的值进行谓词?(如何?我现在看了很多,找不到任何东西)
谢谢你的帮助。
PS:如果有帮助,这是我用来尝试解析数据的代码,它首先包含在字符串中self.material.Text
:
NSData * data = [self.material.Text dataUsingEncoding:NSUnicodeStringEncoding];
TFHpple * tableParser = [TFHpple hppleWithHTMLData:data];
NSString * firstXPath = @"/div";
NSArray<TFHppleElement *> * tableHeader = [tableParser searchWithXPathQuery:firstXPath];
NSLog(@"We found : %d", tableHeader.count);