Hpple使用Objective-C解析HTML [英] Hpple parsing HTML with Objective-C

查看:137
本文介绍了Hpple使用Objective-C解析HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循 RayWendErlich 的教程解析HTML节点。



我从 index.html 获取内容。
我尝试使用此方法获取背景值。

  + (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 *节点中的元素){
Model * model = [[Model alloc] init];
model.colorString = [element objectForKey:@style];
[resultArray addObject:model];
//NSLog(@\"%@\",model.colorString);






所以问题是:



我做错了什么?

解决方案

在您的代码中出现小错误。


  1. 您使用的xPath不正确。您在 class 前缺少 @

  2. code> background key是一个属性,所以你需要为 TFHppleElement 查询它的属性 property(它是一个字典),并通过 objectForKey:获取它的值。

这是最终的代码:

  NSArray * nodes = [parser searchWithXPathQuery:@// div [类= '内容'] / DIV / DIV]; 
$ b $ for(TFHppleElement *节点中的元素){
NSLog(@%@,[element.attributes objectForKey:@style]);

$ / code>

控制台输出为:


背景:#D93D59

背景:#E7923D

背景:#768479

背景:#EBBA95 >
背景:#E26967

背景:#BF343F

背景:#254159

背景:#F2F2F2

背景:#D9A577

背景:#BF8969

背景:#04000D

...



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?

解决方案

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

这篇关于Hpple使用Objective-C解析HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆