objective-c - iOS:Html 解析 - 如何忽略 <p> 中的 a、li 等标签

标签 objective-c ios xpath html-parsing hpple

我目前正在使用 Hpple 来解析 HTML,如下所示:

TFHpple *htmlParser = [TFHpple hppleWithHTMLData:[currentString dataUsingEncoding:NSUTF8StringEncoding]];
NSString *paragraphsXpathQuery = @"//p//text()";
        NSArray *paragraphNodes = [htmlParser searchWithXPathQuery:paragraphsXpathQuery];
        if ([paragraphNodes count] > 0) {
            NSMutableArray *tempArray = [NSMutableArray array];
            for (TFHppleElement *element in paragraphNodes) {
                [tempArray addObject:[element content]];
            }
            article.paragraphs = tempArray;
        }

这样我得到了一个段落数组,我可以使用 NSString *result = [myArray componentsJoinedByString:@"\n\n"]; 将它编译成一个文本主体,其中包含一行休息。

但是,如果 html 包含标签,它们将被解释为单独的实体并且会自行换行,所以在一天结束时从这样的一行开始:

<p>I went to the <a href="blablabla.html">shop</a> to get some milk!</a></p>
<p>It was awesome.</p>

我明白了:

I went to the

shop

to get some milk!

It was awesome!

当然我想得到这个(忽略 p 标签内的其他标签):

I went to the shop to get some milk!

It was awesome!

你能帮帮我吗?

最佳答案

NSString *HTMLTags = @"<[^>]*>"; //regex to remove any html tag

NSString *htmlString = @"<html>bla bla</html>";
NSString *stringWithoutHTML = [hstmString stringByReplacingOccurrencesOfRegex:myregex withString:@""];

不要忘记在您的代码中包含这个:#import "RegexKitLite.h"这里是下载这个 API 的链接:http://regexkit.sourceforge.net/#Downloads

关于objective-c - iOS:Html 解析 - 如何忽略 <p> 中的 a、li 等标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12424920/

相关文章:

php - 仅抓取网页的主要内容(忽略页眉、页脚和侧边栏)

.net - 实现解析器的步骤和相关工作(在.Net中,在本例中为XPath 2.0)

java - 如何检测XPath代表的是叶节点或者节点集?

objective-c - 创建 UIImageView

objective-c - 如何观察 NSOperationQueue 的 "operations"属性?

Ipad 上的 javascript window.scrollto 问题

手势处理程序中的iOS绘图上下文NULL

objective-c - 如何对从 UIButton 获取的常量变量执行操作?

ios - 使用目标选择器保留循环

iOS 中的 MySQL 和核心数据