无法通过Hpple解析获取detailviewcontroller上的数据

时间:2015-01-08 01:28:00

标签: html ios objective-c parsing hpple

我是Obj-C的初学者,我尝试使用this raywenderlich tutorial制作应用。

我用文章标题(mininglife.ru)解析html页面,同时我得到每篇文章的网址,并将其保存在NSMutableArray中,以便在DetailviewController中使用它。

但是当我试图将网址拉下并放入NSUrl时,没有任何反应。我已经尝试了很多方法,你能帮助我,并试着解释我是否做错了什么。谢谢。

-(void)loadArticles{

   Articlelist *urlOfArticle = [_objects objectAtIndex:self]; // from previous parsing

   NSURL *articleUrl = [NSURL URLWithString:urlOfArticle.url];// I think this string is wrong, or?
   NSData *htmlUrlsData = [NSData dataWithContentsOfURL:articleUrl];

   TFHpple *articleParser = [TFHpple hppleWithHTMLData:htmlUrlsData];

   NSString *articleNameXpath = @"/html/body/div[1]/div/div[1]/main/article/h1";
   // NSString *articleContentXpath = @"//div[@class'entry-content']//p";

   NSArray *articleNameNodes = [articleParser searchWithXPathQuery:articleNameXpath];
   // NSArray *articleContentNodes = [articleParser searchWithXPathQuery:articleContentXpath];
   NSMutableArray *newArticleArray = [[NSMutableArray alloc] initWithCapacity:0];

   for (TFHppleElement *element in articleNameNodes) {
       Article *newArticleName = [[Article alloc] init];
       [newArticleArray addObject:newArticleName];

       newArticleName.name = [[element firstChild] content]; 
   }

   _articleName = newArticleArray;

   [self.tableView reloadData];
}

1 个答案:

答案 0 :(得分:0)

将界面更改为

@interface MasterViewController () {
    NSMutableArray *_articles;
}
@end

将此添加到MasterViewController

- (void)loadArticles
{
    NSURL *articlesUrl = [NSURL URLWithString:@"http://mininglife.ru"];
    NSData *articlesHtmlData = [NSData dataWithContentsOfURL:articlesUrl];
    TFHpple *articlesParser = [TFHpple hppleWithHTMLData:articlesHtmlData];
    NSString *articlesXpathQueryString = @"/html/body/div[1]/div/div[1]/main/article/h1";

    NSArray *articlesNodes = [articlesParser searchWithXPathQuery:articlesXpathQueryString];

    NSMutableArray *newArticles = [[NSMutableArray alloc] initWithCapacity:0];
    for (TFHppleElement *element in articlesNodes) {
        Article *article = [[Article alloc] init];
        [newArticles addObject:article];
        article.title = [[element firstChild] content];
        article.url = [element objectForKey:@"href"];
    }
    _articles = newArticles;
    [self.tableView reloadData];
}

将DetailviewController.h的界面更改为

@class Article;
@interface DetailViewController : UIViewController
    @property (strong, nonatomic) Article *article;
    @property (strong, nonatomic) IBOutlet UILabel *detailDescriptionLabel;
@end

将DetailViewController.m更改为

#import "DetailViewController.h"
#import "Article.h"
#import "TFHpple.h"

@implementation DetailViewController

@synthesize detailDescriptionLabel = _detailDescriptionLabel;

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self loadArticle];
}

- (void)loadArticle
{
    NSURL *articleUrl = [NSURL URLWithString:self.article.url];
    NSData *articleHtmlData = [NSData dataWithContentsOfURL:articleUrl];

    TFHpple *articleParser = [TFHpple hppleWithHTMLData:articleHtmlData];
    NSString *articleXpathQueryString = @"//div[@class'entry-content']//p";

    NSArray *articleNodes = [articleParser searchWithXPathQuery:articleXpathQueryString];

    NSMutableArray *newArticleContents = [[NSMutableArray alloc] initWithCapacity:0];
    for (TFHppleElement *element in articleNodes) {
        [newArticleContents addObject:[[element firstChild] content]];
    }
    NSLog(@"%@", newArticleContents);
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = NSLocalizedString(@"Detail", @"Detail");
    }
    return self;
}

@end

不要忘记在MasterViewController中将所有对loadTutorials的调用更改为loadArticles。 PS:此示例仅将文章内容打印到控制台

您的xPath也可能出错