2012-11-04 64 views
0

我想解析一個有很多表格的HTML頁面。我搜索瞭如何使用Objective C解析HTML,並發現了hpple。我會找一個教程,導致我:如何解析HTML中的表格?

http://www.raywenderlich.com/14172/how-to-parse-html-on-ios

有了這個教程中,我試圖解析一些論壇新聞裏面有很多表,從該網站(希伯來文):news forum

我試圖解析新聞標題,但我不知道在我的代碼中寫什麼。每次我嘗試到達我得到的路徑時,「節點都是零」。

我的最新嘗試的代碼是:

NSURL *contributorsUrl = [NSURL URLWithString:@"http://rotter.net/cgi-bin/listforum.pl"]; 
NSData *contributorsHtmlData = [NSData dataWithContentsOfURL:contributorsUrl]; 

// 2 
TFHpple *contributorsParser = [TFHpple hppleWithHTMLData:contributorsHtmlData]; 

// 3 
NSString *contributorsXpathQueryString = @"//body/div/center/center/table[@cellspacing=0]/tbody/tr/td/table[@cellspacing=1]/tbody/tr[@bgcolor='#FDFDFD']/td[@align='right']/font[@class='text15bn']/font[@face='Arial']/a/b"; 
NSArray *contributorsNodes = [contributorsParser searchWithXPathQuery:contributorsXpathQueryString]; 

// 4 
NSMutableArray *newContributors = [[NSMutableArray alloc] initWithCapacity:0]; 
for (TFHppleElement *element in contributorsNodes) { 
    // 5 
    Contributor *contributor = [[Contributor alloc] init]; 
    [newContributors addObject:contributor]; 

    // 6 

有人能指導我要得到冠軍?

回答

0

不知道如果這是你的選擇,但如果需要的表有唯一的ID,你可以使用一個混亂的方法:即HTML加載到一個UIWebView並通過– stringByEvaluatingJavaScriptFromString:這樣得到的內容:

// desired table container's id is "msg" 
NSString* value = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementById('msg').innerHTML"];