无法使用 Hpple 解析在详细视图控制器上获取数据



我是 Obj-C 的初学者,我尝试使用这个 raywenderlich 教程制作一个应用程序。

我解析带有文章标题 (mininglife.ru) 的 html 页面,我还获得了每篇文章的 url 并将其保存在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];
}

将接口更改为此

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

将此添加到主视图控制器

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

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

您的 xPath 也可能是错误的

最新更新