hpple
中是否可以递归只获取子元素的所有文本内容? TFHppleElement
类中有任何方法吗?
例如 JavaScript
document.getElementById("testdiv").textContent
最佳答案
我正在使用这段代码获取新闻标题的所有内容
NSURL *newURL = [NSURL URLWithString:@"http://somesite"];
NSData *newsData = [NSData dataWithContentsOfURL: newURL];
TFHpple *newsParser = [TFHpple hppleWithHTMLData: newsData];
NSString *newsXpathQueryString = @"//div[@class='item column-1']";
NSArray *newsNodes = [newsParser searchWithXPathQuery: newsXpathQueryString];
NSMutableArray *newNews = [[NSMutableArray alloc] initWithCapacity: 0];
for (TFHppleElement *element in newsNodes)
{
News *news = [[News alloc] init];
[newNews addObject: news];
news.title = [[element content] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
news.photo_url = [element objectForKey:@"src"];
_allNews = newNews;
[self.tableView reloadData];
}
}
你可以使用
news.title = [[element firstChild]content] to get children elements content
关于ios - hpple:是否有可能获得像 javascript textContent 这样的文本值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29546753/