htmliosobjective-cxpathhpple

Hpple parsing HTML with Objective-C


I've follow the tutorial from RayWendErlich to parse HTML node.

I get the content from an index.html. I've try to use this method to fetch the background value.

+ (void)parseWithHTMLString:(NSString *)string
{
  NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
  TFHpple *parser = [TFHpple hppleWithData:data isXML:NO];

  NSString *XpathQueryString = @"//div[class='content']/div/div";
  NSArray *nodes = [parser searchWithXPathQuery:XpathQueryString];
  NSLog(@"%@",nodes);

  NSMutableArray *resultArray = [[NSMutableArray alloc] initWithCapacity:0];
  for (TFHppleElement *element in nodes) {
    Model *model = [[Model alloc] init];
    model.colorString = [element objectForKey:@"style"];
    [resultArray addObject:model];
    //NSLog(@"%@",model.colorString);
  }
}

So the question is:

What I had done wrong?


Solution

  • There are 2 small errors in your code.

    1. The xPath you used is not correct. You're missing an @ in front of class.
    2. The background key is an attribute, so you need to ask the TFHppleElement for its attributes property (which is a dictionary) and get its value via objectForKey:.

    This is the final code:

    NSArray *nodes = [parser searchWithXPathQuery:@"//div[@class='content']/div/div"];
    
    for (TFHppleElement *element in nodes) {
        NSLog(@"%@",[element.attributes objectForKey:@"style"]);
    }
    

    The console output is:

    background: #D93D59
    background: #E7923D
    background: #768479
    background: #EBBA95
    background: #E26967
    background: #BF343F
    background: #254159
    background: #F2F2F2
    background: #D9A577
    background: #BF8969
    background: #04000D
    ...