【发布时间】:2012-11-23 21:09:58
【问题描述】:
我正在考虑构建一个显示月刊的应用程序。期刊没有 XML,他们只是每个月更改 PDF 的标题标题和 URL。这始终存储在源代码中的相同位置,因此我正在寻找在
中查找所有文本div class=entry clearfix post /div
标记,然后提取第一个 URL。我以前研究过解析 XML,但从来没有解析过 HTML。我最好的选择是什么?
更新:
只有在源代码中的某一点,页面才会显示To Download the PDF, click here。所以,我设置了以下扫描仪:
NSURL *url = [NSURL URLWithString:@"http://www.thejenkinsinstitute.com/Journal/"];
NSString *content = [NSString stringWithContentsOfURL:url];
NSString * aString = content;
NSMutableArray *substrings = [NSMutableArray new];
NSScanner *scanner = [NSScanner scannerWithString:aString];
[scanner scanUpToString:@"<p>To Download the PDF, <a href=\"http://michaelwhitworth.com/wp-content/HE22.pdf\">" intoString:nil]; // Scan all characters before #
while(![scanner isAtEnd]) {
NSString *substring = nil;
[scanner scanString:@"<p>To Download the PDF, <a href=\"" intoString:nil]; // Scan the # character
if([scanner scanUpToString:@"\"" intoString:&substring]) {
// If the space immediately followed the #, this will be skipped
[substrings addObject:substring];
}
[scanner scanUpToString:@"#" intoString:nil]; // Scan all characters before next #
}
NSLog(@"Here is the Substring%@", substrings);
// do something with substrings
[substrings release];
在控制台中,首先返回的是 PDF 的 URL,但它包含更多内容。这是一个简短的摘录。
"2012-11-23 15:33:36.383 Jenkins[8306:c07] Here is the Substring(
"http://michaelwhitworth.com/wp-content/HE22.pdf",
"#8220;As the Bible School Goes So Goes the Congregation” by Ira North</a></p>\n<p style=","
我做错了什么来阻止它只给我 URL,仅此而已?
【问题讨论】:
-
HTML 抓取是一场噩梦,并且是保持事物维护的持续工作的可靠来源。