當使用Xcode中的hpple解析網頁時,我遇到了編碼問題。XCode,iOS:使用hpple解析HTML編碼
- (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的源碼指出,但我得到更奇怪的字符。
如何強制更改數據編碼?任何幫助將不勝感激!
我與NSUTF8StringEncoding也嘗試過,但編碼不改變,有趣的字符都還在這裏:( – b4d
我試圖複製整個table to [link](http://b4d.sablun.org/xpath.html),如果我解析這個鏈接,UTF-8編碼被正確讀取,但如果我解析原始網站UTF-8中斷。 – b4d