所以我想这是我最后一个Hpple问题!我在用Hpple解析的HTML文档中找到了一个条目。我尝试了许多不同的查询,但没有成功。下面是HTML的一个示例。

我可以使用//div@class = 'entry-content'/p获取以"Today's project“开头的文本,也可以使用//div@class = 'entry-content'//a@title//*获取下一个标记以及后面的所有文本。但是,正如您所看到的,在"/span“之后仍然有一些文本。但是,我尝试过的方法都不起作用。我尝试过查看元素的子元素,the //div@class = 'entry-content'/p//text(),//div@class = 'entry-content'/p//following::*,什么都不起作用。如果谁有什么想法,我洗耳恭听!再次感谢您花了这么多时间。
编辑#1,当我尝试不同的东西时,我正在查看HTML。P标记下是我需要的文本,"Today's project...“然后是更改文本颜色并包含链接的跨度,然后是更多文本。我需要做的是跳过这个跨度,继续阅读文本。也许我的问题应该是,如何跨越跨度?谢谢你的关注。
编辑#2好吧,我要开始悬赏这个了。我真的需要一些帮助。我到处都找过了,尝试过很多不同的东西。但对我来说什么都不管用。在那个封闭的跨度之后,我无法获取文本。这种格式经常出现。博客的作者,我正在为应用程序解析这个,有时会改变她的文字风格,在她改变风格后,我无法获得文本。任何帮助都将不胜感激。再次感谢您的关注。
编辑#3这里是DOM树HTML的另一个屏幕截图。如果您注意到我正在解析div类"entry content“,那么问题中的文本就会暴露出来。以"Today...“开头然后改变文本的跨度,我就可以得到这个文本了。这是后面的文字,我需要,“这是一个……”就在结束p标记之前。

我还把整个HTML放在了gist上。HERE。所讨论的线路是102。尽管HTML没有很好地复制它。谢谢。
发布于 2013-05-28 03:51:24
在代码中做一些更改以进一步了解层次结构,它在您的html示例上起作用。注意:为了方便起见,我将所有的条目内容添加到一个NSMutableString中。正如我在comment,中警告过的那样,请谨慎使用它。:-)
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"html"];
NSData *data = [NSData dataWithContentsOfFile:filePath];
TFHpple *detailParser = [TFHpple hppleWithHTMLData:data];
NSString *xpathQueryString = @"//div[@class='entry-content']";
NSArray *node = [detailParser searchWithXPathQuery:xpathQueryString];
NSMutableString *test = [[NSMutableString alloc] initWithString:@""];
for (TFHppleElement *element in node) {
for (TFHppleElement *child in element.children) {
if (child.content != nil) {
[test appendString:child.content];
}
if ([child.children count]!= 0) {
for (TFHppleElement *grandchild in child.children) {
if (grandchild.content != nil) {
[test appendString:grandchild.content];
}
for (TFHppleElement *greatgrandchild in grandchild.children) {
if (greatgrandchild.content != nil) {
[test appendString:greatgrandchild.content];
}
for (TFHppleElement *greatgreatgrandchild in greatgrandchild.children) {
if (greatgreatgrandchild.text != nil) {
[test appendString:greatgreatgrandchild.text];
}
if (greatgreatgrandchild.content != nil) {
[test appendString:greatgreatgrandchild.content];
}
}
}
}
}
}
}
NSLog(@"test = %@", test);发布于 2013-05-23 23:04:16
可以叫我“生手”,但你可以直接把代码读成一个字符串,然后用你要找的标签把它分解成一个数组。这可以在PHP/Javascript/等中完成。然后,您只需提取包含您要查找的文本的数组元素即可。不需要花哨的/外部的东西。
示例:
$string = '<p>text is here</p><p>more text is here</p>';
$string = explode('<p>', $string);
Now $string = [0] => "text is here</p>", [1] => "more text is here</p>"https://stackoverflow.com/questions/16671987
复制相似问题