【问题标题】:Extract First URL Within Certain Div class提取某些 Div 类中的第一个 URL
【发布时间】: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&#8221; by Ira North</a></p>\n<p style=","

我做错了什么来阻止它只给我 URL,仅此而已?

【问题讨论】:

  • HTML 抓取是一场噩梦,并且是保持事物维护的持续工作的可靠来源。

标签: ios html nsstring extract


【解决方案1】:

我做了类似的事情,我建立了一个小型 Web 服务(API,它基本上是一个简单的 Ruby 应用程序,它正在报废我需要的 html,并以 REST 方式返回它。Web 服务/API 是个好主意因为如果 HTML 中发生任何变化(例如 id 的元素变化),您不必更新 iOS 应用程序来更改正在解析的节点的路径。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-17
    • 1970-01-01
    • 2018-04-01
    • 1970-01-01
    • 2013-04-27
    相关资源
    最近更新 更多