Getting last <p> tag of HTML document
I have an HTML document, for example:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body style="font-family: Geneva; color: rgb(0, 0, 0); font-size: 12px; word-wrap: break-word; font-weight: normal; 开发者_开发问答font-style: normal; text-decoration: none; ">
<p style="font-family: LucidaGrande; color: rgb(51, 102, 204); margin-top: 6px; margin-bottom: 6px; word-wrap: break-word; font-weight: normal; font-style: normal; text-decoration: none; ">
fdskl says: (6:50:04 AM)
</p>
<p style="font-family: Arial-ItalicMT; color: rgb(0, 0, 0); margin-left: 36px; margin-top: 6px; margin-bottom: 6px; word-wrap: break-word; font-weight: normal; font-style: normal; text-decoration: none; ">
Hello
</p>
<p style="font-family: LucidaGrande; color: rgb(51, 102, 204); margin-top: 6px; margin-bottom: 6px; word-wrap: break-word; font-weight: normal; font-style: normal; text-decoration: none; ">
fdskl says: (6:50:18 AM)
</p>
<p style="font-family: Arial-ItalicMT; color: rgb(0, 0, 0); margin-left: 36px; margin-top: 6px; margin-bottom: 6px; word-wrap: break-word; font-weight: normal; font-style: normal; text-decoration: none; ">
How are you?
</p>
</body>
</html>
and I want to get whatever is inside the last p tag of this HTML. So in this case, it would be "How are you?". Using Cocoa, how can I do that? Thanks!
Your best option is to use NSXMLDocument
:
NSData *htmlData = ... // get the html data, preferably asynchronously
NSXMLDocument *document = [[[NSXMLDocument alloc] initWithData:htmlData options:NSXMLDocumentTidyHTML error:NULL] autorelease];
NSArray *nodes = [document nodesForXPath:@"//body/p" error:NULL];
NSXMLNode *lastP = [nodes lastObject];
NSLog(@"%@", [lastP stringValue]);
You should also check for errors instead of passing NULL if you want your code to be more robust.
精彩评论