Iphone-TFHpple- having trouble with Xpath for HTML
I'm new to objective c开发者_运维百科 and trying to parse HTML with TFHpple, and having trouble with Xpath.
Currently I'm trying to get news topic from main page of Yahoo Japan. but it return nil.
NSStringEncoding UTF = 4;
NSString *htmlWillInsert = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.yahoo.co.jp/"] encoding:UTF error:nil];
NSData *htmlData = [htmlWillInsert dataUsingEncoding:NSUnicodeStringEncoding];
TFHpple *xpathParser = [[TFHpple alloc] initWithHTMLData:htmlData];
NSArray *elements = [xpathParser searchWithXPathQuery:@"/html/body/div/div[2]/div[3]/div/div/div[2]/div/div/ul/li/a"];
NSLog(@"%@", elements);
for (TFHppleElement *element in elements)
{
NSLog(@"%@", [element content]);
}
[xpathParser release];
i could get html from yahoo.com . but can't get anything from yahoo JP.
dose any1 know the solution?
As yahoo.jp is in html and not xml, you can't parse it as such.
To parse HTML, you can see the examples given in this post : parsing HTML on the iPhone
精彩评论