在 XCode 中使用 hpple 解析网页时出现编码问题。
- (void)loadTutorials {
NSURL *tutorialsUrl = [NSURL URLWithString:@"http://qrz.si/members/s55db/"];
NSData *tutorialsHtmlData = [NSData dataWithContentsOfURL:tutorialsUrl options:NSASCIIStringEncoding error:nil];
TFHpple *tutorialsParser = [TFHpple hppleWithHTMLData:tutorialsHtmlData];
NSString *tutorialsXpathQueryString = @"//td[@class='data']";
NSArray *tutorialsNodes = [tutorialsParsersearchWithXPathQuery:tutorialsXpathQueryString];
NSMutableArray *newTutorials = [[NSMutableArray alloc] initWithCapacity:0];
for (TFHppleElement *element in tutorialsNodes) {
Tutorial *tutorial = [[Tutorial alloc] init];
[newTutorials addObject:tutorial];
for (TFHppleElement *child in element.children) {
if ([child.tagName isEqualToString:@"img"]) {
// NSLog([child objectForKey:@"src"]);
} else if ([child.tagName isEqualToString:@"p"]) {
//NSLog([[child firstChild] content]);
tutorial.title = [[child firstChild] content];
}
}
}
_objects = newTutorials;
[self.tableView reloadData];
}
正如消息来源所指出的那样,页面应该是 UTF-8,但我得到了奇怪的字符。
如何强制更改数据的编码?任何帮助将不胜感激!