سؤال

What I am trying to do is parse multiple sections of a page on a site. Here is what I am doing:

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]);
    }
}

The console logs 5 different correct results, but as you can see my code is quite tedious. So how can I get all the contents of the multiple tags?

Thanks!

هل كانت مفيدة؟

المحلول

Hpple uses XPathQuery to parse HTML. If you want to search for any class you can use:

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

or if you only want those specific classes you can try:

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

The syntax is documented here

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top