iphone - 从 iPhone 字符串中提取 HTML 属性

标签 iphone objective-c ios

我有一个从网站响应中获得的 html 字符串。我在那里所做的一切都很棒,而且没有任何困难。我需要做的是获取 html 中的 only href 属性。获取该属性中包含的 URL 的最佳方法是什么?如果有必要,我对任何外部库开放,我只想要尽可能最有效的方法。谢谢。

最佳答案

使用此 API 解析 HTML 代码并选择所需的元素。

ElementParser 是一个轻量级框架,可以轻松访问 xml 和 html 内容。它不想迷失在 HTML 和 XML 规范的复杂性中,而是希望不掩盖它们本质上的简单性。它不会做所有事情,它渴望做“恰到好处”。

来源:http://touchtank.wordpress.com/element-parser/


这里是如何将 ElementParser 与您自己的示例一起使用的示例。我希望这对您有所帮助。

圣诞快乐!嗬嗬嗬

// Here you create the parser, don't forget to #import "Element.h" and #import "ElementParser.h"
ElementParser * parser = [[ElementParser alloc] init];

// This is the HTML source code that you want to parse
DocumentRoot* document = [parser parseHTML:@"<html><a href=\"http://google.com\">Google Link</a></html>"];

// Create an array where you will put all the <a></a> elements
NSArray* elements = [document selectElements: @"a"];

// Iterate though the array, for each element pick the "href" attribute
NSMutableArray* results = [NSMutableArray array];
for (Element* element in elements){
    NSString* snipet = [element attribute:@"href"];

    // Add the result for each element to the "results" array
    [results addObject: snipet];
}

// Print the results on the screen
NSLog(@"%@",[results componentsJoinedByString: @"\n"]);

关于iphone - 从 iPhone 字符串中提取 HTML 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8622741/

相关文章:

iphone - 阻止 UIAlertViewDelegate

iphone - 游戏套件中回合制多人游戏的问题

ios - AVMetadataObject 类型 PDF417 代码无法读取马里兰州驾照

ios - 将自定义颜色设置为 UITabBarItem 不起作用

ios - 如何更改 UITableview 索引栏选定索引的颜色

iphone - 如何取消touchesMoved :withEvent:

iphone - 如何通过单击 "Paid"应用程序中的按钮在iPhone的AppStore中打开 "Lite"应用程序页面?

ios - ? : in Objective-C

iphone - 在 iOS 中移除键盘的阴影

ios - 如何在 reloadData() 之后为 UITableViewCell 中的 UILabel 设置动画?