使用hpple解析数据,然后在标签中显示

时间:2014-03-31 01:53:47

标签: parsing label hpple

到目前为止,我有这个:

我正在尝试用网页中的hpple解析日期,然后将其显示在标签中。我不知道怎么回事。我想我必须使void GoToSecretsList中的Secret变量以某种方式等于其他void得到的值...有没有人有任何建议?

- (void)GoToSecretsList
{
UIViewController *vc;

vc = [self.storyboard instantiateViewControllerWithIdentifier:@"SecretsListID"];
[self.navigationController pushViewController:vc animated:YES];

UILabel *myLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 120, 500, 40)];
[myLabel setBackgroundColor:[UIColor clearColor]];

NSString *Secret;
Secret = @"Get value from website";

[myLabel setText:[NSString stringWithFormat:@"#1: %@", Secret]];


[[vc view] addSubview:myLabel];

}

 -(void)LoadSecrets {
// 1
NSURL *SecretsUrl = [NSURL URLWithString:@"http:/removed.com/xlxlx"];
NSData *SecretsHtmlData = [NSData dataWithContentsOfURL:SecretsUrl];

// 2
TFHpple *secretsParser = [TFHpple hppleWithHTMLData:SecretsHtmlData];

// 3
NSString *secretsXpathQueryString = @"/secret";
NSArray *secretsNodes = [secretsParser searchWithXPathQuery:secretsXpathQueryString];

// 4
NSMutableArray *newSecrets = [[NSMutableArray alloc] initWithCapacity:0];
for (TFHppleElement *element in secretsNodes) {
    // 5
    VOI *secret = [[VOI alloc] init];
    [newSecrets addObject:secret];

    // 6
    secret.title = [[element firstChild] content];

    // 7
    secret.url = [element objectForKey:@"href"];
}

 }

编辑:现在我有这个:

 - (void)GoToSecretsList
 {
UIViewController *vc;

vc = [self.storyboard instantiateViewControllerWithIdentifier:@"SecretsListID"];
[self.navigationController pushViewController:vc animated:YES];

UILabel *myLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 120, 500, 40)];
[myLabel setBackgroundColor:[UIColor clearColor]];


NSURL *SecretsUrl = [NSURL URLWithString:@"http://removed.com/dsdsds"];
NSData *SecretsHtmlData = [NSData dataWithContentsOfURL:SecretsUrl];


TFHpple *secretsParser = [TFHpple hppleWithHTMLData:SecretsHtmlData];


NSString *secretsXpathQueryString = @"/secret";
NSArray *secretsNodes = [secretsParser searchWithXPathQuery:secretsXpathQueryString];


[myLabel setText:[NSString stringWithFormat:@"#1: %@", secretsNodes]];
[[vc view] addSubview:myLabel];

}

但是什么变量有信息..?或者我做错了什么?因为secretNodes不是它。

1 个答案:

答案 0 :(得分:0)

你能发布一些真正的HTML来帮助你知道你在找什么文字吗?

e.g。以下内容将获取所有<h2></h2>个节点:

NSString *secretsXpathQueryString = @"//h2";//note double slashes for tag name

然而这会得到具有特定类别的人:

NSString *secretsXpathQueryString = @"//h2[@class='secret']";//note single quotes

获得正确的查询后,请查看TFHppleElement.h以查看您可能会调查的属性,例如: element.firstChild.content