0

到目前为止,我有这个:

我正在尝试使用网页中的 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 不是。

4

1 回答 1

0

您可以发布一些真正的 html 来帮助了解您要查找的文本吗?

例如以下将获取所有<h2></h2>节点:

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

而这将获得具有特定类别的人:

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

一旦你有正确的查询,看看TFHppleElement.h 你可能会调查哪些属性,例如: element.firstChild.content

于 2014-10-28T12:56:11.087 回答