2013-04-16 84 views
1

我想解析:[www.neiu.edu/~neiutemp/PhoneBook/alpha.htm]使用TFHPPLE分析器,我正在尋找從每個TR(行第一列) )在一張桌子裏。這裏所有的TD屬性都是一樣的。我無法區分TD。
我能夠獲得所有的HTML代碼,但未能從每個TR獲得第一個TD。在// 3之後(在代碼中)tutorialsNodes爲空。的HTML表解析xcode

NSLog(@"Nodes are : %@",[tutorialsNodes description]); 

輸出是

 
Practice1[62351:c07] Nodes are :(). 

我看不出有什麼不對。任何幫助,將不勝感激。 我的代碼來解析這個URL:

NSURL *tutorialsUrl = [NSURL URLWithString:@"http://www.neiu.edu/~neiutemp/PhoneBook/alpha.htm"]; 
NSData *tutorialsHtmlData = [NSData dataWithContentsOfURL:tutorialsUrl]; 

// 2 
TFHpple *tutorialsParser = [TFHpple hppleWithHTMLData:tutorialsHtmlData]; 

// 3 
NSString *tutorialsXpathQueryString = @"//TR/TD"; 
NSArray *tutorialsNodes = [tutorialsParser searchWithXPathQuery:tutorialsXpathQueryString]; 
NSLog(@"Nodes are : %@",[tutorialsNodes description]); 
// 4 
NSMutableArray *newTutorials = [[NSMutableArray alloc] initWithCapacity:0]; 
for (TFHppleElement *element in tutorialsNodes) { 
    // 5 
    Tutorial *tutorial = [[Tutorial alloc] init]; 
    [newTutorials addObject:tutorial]; 

    // 6 
    tutorial.title = [[element firstChild] content]; 

    // 7 
    tutorial.url = [element objectForKey:@"href"]; 

    NSLog(@"title is: %@",[tutorial.title description]); 
} 

// 8 
_objects = newTutorials; 
[self.tableView reloadData]; 
+0

的Xcode不解析HTML。這是用於OS X操作系統的iOS嗎?您應該相應地用「使用Cocoa/Cocoa Touch」來解釋這一點。 – 2013-04-16 21:08:02

+0

我改寫了這個問題。 –

回答

2

如果使用@"//tr/td"代替@"//TR/TD"這應該工作。

看着你的HTML,雖然它的作者顯然不知道如何拼寫CSS,但你在源代碼中埋藏了字體標籤。所以,你的下一個行代碼,這顯然是從優秀Hpple tutorial by Matt Galloway on Ray Wenderlich's site拍攝,他說:

tutorial.title = [[element firstChild] content]; 

但是,這不會在這裏工作,因爲對於大多數條目中,firstChild不是text,而是這是一個font標籤。所以,你可以檢查,看它是否是一個font標籤,像這樣:

TFHppleElement *subelement = [element firstChild]; 
if ([[subelement tagName] isEqualToString:@"font"]) 
    subelement = [subelement firstChild]; 
tutorial.title = [subelement content]; 

或者,你可以而不是隻搜索@"//tr/td/font"而不是@"//tr/td"。很多方法在這裏。這個技巧(就像所有的HTML解析一樣)將會使它具有相當的穩健性,所以你不會輕易對頁面進行微小的修飾。

很明顯,您的HTML沒有URL,因此代碼在這裏不適用。

無論如何,我希望這足以讓你走。


您報告有問題,所以我想我只是提供一個更完整的代碼示例:

NSURL *tutorialsUrl = [NSURL URLWithString:@"http://www.neiu.edu/~neiutemp/PhoneBook/alpha.htm"]; 
NSData *tutorialsHtmlData = [NSData dataWithContentsOfURL:tutorialsUrl]; 

TFHpple *tutorialsParser = [TFHpple hppleWithHTMLData:tutorialsHtmlData]; 

NSString *tutorialsXpathQueryString = @"//tr/td"; 
NSArray *tutorialsNodes = [tutorialsParser searchWithXPathQuery:tutorialsXpathQueryString]; 

if ([tutorialsNodes count] == 0) 
    NSLog(@"nothing there"); 
else 
    NSLog(@"There are %d nodes", [tutorialsNodes count]); 

NSMutableArray *newTutorials = [[NSMutableArray alloc] initWithCapacity:0]; 
for (TFHppleElement *element in tutorialsNodes) { 

    Tutorial *tutorial = [[Tutorial alloc] init]; 
    [newTutorials addObject:tutorial]; 

    TFHppleElement *subelement = [element firstChild]; 
    if ([[subelement tagName] isEqualToString:@"font"]) 
     subelement = [subelement firstChild]; 
    tutorial.title = [subelement content]; 

    NSLog(@"title is: %@", [tutorial.title description]); 
} 

這將產生以下的輸出:

 
2013-05-10 19:39:42.027 hpple-test[33881:c07] There are 10773 nodes 
2013-05-10 19:39:42.028 hpple-test[33881:c07] title is: A 
2013-05-10 19:39:46.027 hpple-test[33881:c07] title is: (null) 
2013-05-10 19:39:46.698 hpple-test[33881:c07] title is: (null) 
2013-05-10 19:39:47.333 hpple-test[33881:c07] title is: (null) 
2013-05-10 19:39:47.827 hpple-test[33881:c07] title is: (null) 
2013-05-10 19:39:48.358 hpple-test[33881:c07] title is: (null) 
2013-05-10 19:39:49.133 hpple-test[33881:c07] title is: (null) 
2013-05-10 19:39:49.775 hpple-test[33881:c07] title is: Abay, Hiwet B 
2013-05-10 19:39:50.326 hpple-test[33881:c07] title is: H-Abay 
2013-05-10 19:39:50.992 hpple-test[33881:c07] title is: 773-442-5140 
2013-05-10 19:39:51.597 hpple-test[33881:c07] title is: (null) 
2013-05-10 19:39:52.092 hpple-test[33881:c07] title is: Controller 
2013-05-10 19:39:52.598 hpple-test[33881:c07] title is: E 
2013-05-10 19:39:53.149 hpple-test[33881:c07] title is: 223 
2013-05-10 19:39:55.040 hpple-test[33881:c07] title is: Abbruscato, Terence 
2013-05-10 19:39:55.806 hpple-test[33881:c07] title is: T-Abbruscato 
2013-05-10 19:39:56.525 hpple-test[33881:c07] title is: 773-442-5339 
... 
+0

非常感謝你Rob。有用。但是在:** NSLog(@「標題是:%@」,[tutorial.title description]); **,我得到了:** 2013-05-10 14:23:15.375 Practice1 [78352:c07] title is :(null)**作爲輸出。我用tr/td/font作爲xpathQueryString。請多多建議! –

+0

@MitraPatel我用更完整的代碼示例更新了我的答案。 – Rob

+0

我也有同樣的感謝。 –