2011-08-02 81 views

回答

2

该代码取决于libxml2,基本上是一个薄包装,它提供了一个更方便的接口来解析HTML与目标C.这只在iOS 3.1.3上测试& 3.2,如果你使用的是OSX,可能更好的调查使用WebKit操纵你的DOM。以下代码可能会对您有所帮助。

//Example to download google's source and print out the urls of all the images 
NSError * error = nil; 
HTMLParser * parser = [[HTMLParser alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://www.google.com"] error:&error]; 
if (error) { 
    NSLog(@"Error: %@", error); 
    return; 
} 

HTMLNode * bodyNode = [parser body]; //Find the body tag 
NSArray * imageNodes = [bodyNode findChildTags:@"img"]; //Get all the <img alt="" /> 
for (HTMLNode * imageNode in imageNodes) { //Loop through all the tags 
    NSLog(@"Found image with src: %@", [imageNode getAttributeNamed:@"src"]); //Echo the src="" 
} 

[parser release]; 

This libxml wrapper for Objective C也可能有用。

+0

哇.. 这就是我想要的结果 感谢您的帮助,HonestSuccess。 你能帮助我吗? 如果我还没有libxml2框架。 我该怎么做,并得到结果? – rbbtsn0w

+0

好的..谢谢..我只是怎么做到的 – rbbtsn0w