【发布时间】:2015-03-28 01:39:06
【问题描述】:
我正在尝试创建一个 iOS 应用程序,只是为了提取网页的一部分。
我的代码可以连接到 URL 并将 HTML 存储在 NSString 中
我已经尝试过了,但我的结果只是得到空字符串
NSScanner* newScanner = [NSScanner scannerWithString:htmlData];
// Create a new scanner and give it the html data to parse.
while (![newScanner isAtEnd])
{
[newScanner scanUpToString:@"<body>" intoString:NULL];
// Scam until <body> tag is found
[newScanner scanUpToString:@"</body>" intoString:&bodyText];
// Everything up to the end tag will get placed into the memory address of the result string
}
我已经尝试了另一种方法...
NSScanner* newScanner = [NSScanner scannerWithString:htmlData];
// Create a new scanner and give it the html data to parse.
while (![newScanner isAtEnd])
{
[newScanner scanUpToString:@"<body" intoString:NULL];
// Scam until <body> tag is found
[newScanner scanUpToString:@">" intoString:NULL];
// Go to end of opening <body> tag
[newScanner scanUpToString:@"</body>" intoString:&bodyText];
// Everything up to the end tag will get placed into the memory address of the result string
}
第二种方式返回一个以>< script...等开头的字符串
如果我说实话,我没有一个好的 URL 来测试这个,我认为在删除正文中的标签方面也有一些帮助可能会更容易(比如 <p></p>)
非常感谢任何帮助
【问题讨论】:
-
你在哪里查看
bodyText?在循环内还是之后? -
顺便说一下,如果您正在解析 HTML,您可能需要使用适当的解析器,例如 HPPL。例如,请参阅Ray Wenderlich's How to Parse HTML on iOS。
-
我在这个while循环之后检查了bodyText。我的具体任务是出于教育目的了解如何使用这些功能。我对 Objective C 和 iOS 很陌生,所以我只是想看看我能用这种方式做什么。在我了解了更基本的东西后,我会查看其他库
标签: html ios objective-c nsscanner