0
這裏是我的代碼示例:NSURLSession如何檢索只有標題?
- (void)displayURL {
NSString *myUrlString = @"https://3docean.net/search/apple%20watch?page=1";
NSURL *myUrl = [NSURL URLWithString:myUrlString];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithURL:myUrl completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (!error) {
NSString *htmlString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@", htmlString);
} else {
NSLog(@"error = %@", error.localizedDescription);
}
}];
[dataTask resume];
}
它返回:
我想要檢索和NSLog
只有所有的標題。
你可以嘗試一個HTML解析器(有幾個庫)或'NSRegularExpression'和基準兩種解決方案。 – nathan
看看這個:https://www.raywenderlich.com/14172/how-to-parse-html-on-ios。這可能會幫助你 – 3stud1ant3
如果該網站的Web API以XML或JSON響應,我顯然會更好。否則,它只是更多的自定義/複雜的解析,如果他們更改頁面的HTML代碼,您可能需要重做它。 – Larme