1

だから私は HPPLE を使用して、いくつかのサイトの基本的な Web スクレイピングを行う必要がある iOS アプリでいくつかの Xpath クエリを実行しています。今のところ、すべてうまくいっていますが、私がやっていることをもっとエレガントに行う別の方法があるかどうかを知りたいと思っていました。現在私がやっていることは、XPath を使用して Web サイト内の特定の div クラスを見つけることです。その Web サイト内 (基本的には投稿のようなもの) には、テキストを持つ子とテキストを持つ子がいくつでも存在できます。別の子供のセットに埋もれています。現在、基本的にForループを繰り返し使用して、「テキスト」tagNameが存在するかどうかを確認し、存在する場合はその値を文字列に追加し、存在しない場合は、スキャンする必要がある別のレベルの子があるかどうかを確認し、4つのレベルがありますこれまでのところ同じ検索です。

 for (TFHppleElement *element in searchNodes) {
    //If a Text Node is found add it to the String, if not search again with next layer
    if ([element.tagName isEqualToString:@"text"]) {
        [bigString appendString:element.content];
    }
    //1. First layer Scan
    if (element.children.count > 0) {
        for (TFHppleElement *nextStep in element.children) { 
            if ([nextStep.tagName isEqualToString:@"text"]) {
                [bigString appendString:nextStep.content];
            }
            
            //2. Second layer Scan
            if (nextStep.children.count > 0) {
                for (TFHppleElement *child in nextStep.children) { 
                    if ([child.tagName isEqualToString:@"text"]) {
                        [bigString appendString:child.content];
                        
                    }
                    
                    //3. Thrid Layer Scan
                    if (child.children.count > 0) {
                        for (TFHppleElement *children in child.children) { 
                            if ([children.tagName isEqualToString:@"text"]){
                                [bigString appendString:children.content];
                            }
                            
                            //4. Fourth Layer Scan
                            if (children.children.count > 0) {
                                for (TFHppleElement *newchild in children.children){
                                    if ([newchild.tagName isEqualToString:@"text"]) {
                                        [bigString appendString:newchild.content];
                                        
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

基本的に最初の NSArray を送信して、追加の要素をチェックし、次の配列で再度検索を実行しながら、最終的にすべての検索からのすべてのテキスト。そうでない場合、私が今持っているものはうまく機能しているようです。これを行うためのよりクリーンな方法があるかどうかを確認したかっただけです。

4

1 に答える 1

0

あなたが望むのはrecursionだと思います。要素を渡す再帰メソッドを作成し、NSMutableString を自身の外側 (インスタンス変数でしょうか?) に変更させてから、可能であればそれ自体を呼び出すchildrenことができます。例 (未コンパイル、未テスト):

@property (nonatomic, retain) NSMutableString * bigString;
// snip
@synthesize bigString;
// snip - assume bigString gets initialized somewhere

- (void)checkElement:(TFHppleElement *)elem {
    if ([element.tagName isEqualToString:@"text"]) {
        [bigString appendString:elem.content];
    }

    if (element.children.count > 0) {
        for (TFHppleElement * child in element.children) {
            [self checkElement:child];
        }
    }
}
于 2012-07-11T22:30:46.113 に答える