我想寫一個非常簡單的iOS應用程序,將解析一個網頁(http://arxiv.org/list/cond-mat/recent)並顯示它的簡化版本。我選擇使用TFHpple來解析這個頁面。我想獲得論文的標題並將其顯示在TableViewController中。紙描述的HTML容器看起來像:解析一個網頁與TFHpple
<div class="list-title">
<span class="descriptor">Title:</span> Encoding Complexity within Supramolecular Analogues of Frustrated Magnets
</div>
功能,我用它來解析和獲取值如下(感謝raywenderlich.com):
- (void) loadPapers{
NSURL *papersURL = [NSURL URLWithString:@"http://www.arxiv.org/list/cond-mat/recent"];
NSData *papersHTMLData = [NSData dataWithContentsOfURL:papersURL];
TFHpple *papersParser = [TFHpple hppleWithHTMLData:papersHTMLData];
NSString *papersXpathQueryString = @"//div[@class='list-title']";
NSArray *papersNodes = [papersParser searchWithXPathQuery:papersXpathQueryString];
NSMutableArray *newPapers = [[NSMutableArray alloc] initWithCapacity:0];
for (TFHppleElement *element in papersNodes){
Paper *paper = [[Paper alloc] init];
[newPapers addObject:paper];
paper.title = [[element firstChild] content];
}
_objects = newPapers;
[self.tableView reloadData];
}
這個功能應該解析整個HTML頁面並將數據返回到TableView。但是,當我嘗試它將空對象返回到paperNodes數組。基本上,元素的數量是正確的(〜25),但它們都是空的,我不知道爲什麼。
任何幫助,非常感謝!謝謝!
如果你沒有綁定到tfhpple,你可以試試[HTMLKit](https://github.com/iabudiab/HTMLKit)。讓我知道你是否需要幫助。 – iska
是的,我沒有綁定tfhpple,我只需要訪問div中的文本,然後將這些數據傳入某個容器,稍後再顯示它。你能給你一些有用的鏈接,你可以找到很好的關於HTMLKit的知識嗎? –