Hpple parsing HTML with Objective-C

邮差的信 提交于 2019-12-06 11:49:11

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
...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!