0

我要做的是解析网站上页面的多个部分。这是我正在做的事情:

NSURL *tutorialsUrl = [NSURL URLWithString:@"http://www.cetsp.com.br/"];
NSData *tutorialsHtmlData = [NSData dataWithContentsOfURL:tutorialsUrl];

TFHpple *tutorialsParser = [TFHpple hppleWithHTMLData:tutorialsHtmlData];

NSArray *holder;

NSString *tutorialsXpathQueryString = @"//div[@class='info oeste']/a/h4";
NSString *tutorialsXpathQueryString2 = @"//div[@class='info norte']/a/h4";
NSString *tutorialsXpathQueryString3 = @"//div[@class='info centro']/a/h4";
NSString *tutorialsXpathQueryString4 = @"//div[@class='info leste']/a/h4";
NSString *tutorialsXpathQueryString5 = @"//div[@class='info sul']/a/h4";

NSArray *oeste = [tutorialsParser searchWithXPathQuery:tutorialsXpathQueryString];
NSArray *norte = [tutorialsParser searchWithXPathQuery: tutorialsXpathQueryString2];
NSArray *centro = [tutorialsParser searchWithXPathQuery:tutorialsXpathQueryString3];
NSArray *leste = [tutorialsParser searchWithXPathQuery:tutorialsXpathQueryString4];
NSArray *sul = [tutorialsParser searchWithXPathQuery: tutorialsXpathQueryString5];

holder = [[NSArray alloc] initWithObjects:norte, oeste, centro,leste,sul, nil];

for (NSArray *array in holder) {

    for (TFHppleElement *element in array) {

        NSLog([[element firstChild] content]);
    }
}

控制台记录了 5 个不同的正确结果,但正如您所见,我的代码非常乏味。那么如何获取多个标签的所有内容呢?

谢谢!

4

1 回答 1

5

Hpple 使用 XPathQuery 来解析 HTML。如果你想搜索任何类,你可以使用:

@"//div[@class=*]/a/h4"

或者,如果您只想要那些特定的课程,您可以尝试:

@"//div[@class='info oeste']/a/h4 | //div[@class='info norte']/a/h4 | //div[@class='info centro']/a/h4"

语法记录在这里

于 2012-08-14T14:12:35.250 回答