0

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

我用文章标题 (mininglife.ru) 解析 html 页面,我还获取每篇文章的 url 并将其保存NSMutableArray在我的DetailviewController.

但是,当我尝试将 url 关闭并将其放入时,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];
}
4

1 回答 1

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

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

您的 xPath 也可能是错误的

于 2015-01-11T17:15:08.707 回答