我是objective c的新手,正在尝试用TFHpple解析HTML,但在使用Xpath时遇到了麻烦。
目前我正在尝试从雅虎日本主页获取新闻主题。但它返回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]; 我可以从yahoo.com获取html。但无法从雅虎JP处获得任何信息。
any1知道解决方案吗?
发布于 2011-10-12 22:17:51
因为yahoo.jp是html格式的,而不是xml格式的,所以您不能这样解析它。
要解析HTML语言,您可以查看本文中给出的示例:parsing HTML on the iPhone
https://stackoverflow.com/questions/7735976
复制相似问题