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