0

多くのテーブルを含む HTML ページを解析しようとしています。Objective C で HTML を解析する方法についてネットを検索したところ、hpple が見つかりました。私を導くチュートリアルを探します:

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

このチュートリアルでは、このサイト (ヘブライ語) から多くのテーブルがあるフォーラム ニュースを解析しようとしました:ニュース フォーラム

ニュースのタイトルを解析しようとしましたが、コードに何を記述すればよいかわかりません。パスにたどり着こうとするたびに、「ノードは nil でした」というメッセージが表示されます。

私の最新の試みのコードは次のとおりです。

 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

誰かがタイトルを取得する方法を教えてくれませんか?

4

1 に答える 1

0

それが選択肢かどうかはわかりませんが、必要なテーブルに一意の ID がある場合は、面倒な方法を使用できます。その html を UIWebView にロードし、– stringByEvaluatingJavaScriptFromString:のようにコンテンツを取得します。

// desired table container's id is "msg"
NSString* value = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementById('msg').innerHTML"];
于 2012-11-04T18:09:01.990 に答える