我想从站点html的特定部分中检索数据,并编写了以下代码。不幸的是,该代码无法正常工作。我怎样才能解决这个问题?

html类名称为“ topic_content”,适用站点中的每篇文章均包含该名称(请参见代码中的注释)。

@implementation DetailViewController
....
UIActivityIndicatorView *activityIndicator;

- (void)viewDidLoad {
      .....
      activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
      [activityIndicator stopAnimating];
      ....
      [self callDetailNews];
}

-(void)callDetailNews{
      ....
      if ([[Reachability reachabilityForInternetConnection]currentReachabilityStatus]!=NotReachable)
      {
           // article link :: http://el3en.com/?articles=topic&topic=20840 (token_ID)

           // <td class="topic_content" colspan='6' itemprop="articleBody"  style="text-align:justify;  word-wrap: break-word; width:100%">

           // NSString *someHTML = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByClassName('box')[0].innerHTML;"];

           NSString *articleString = [NSString stringWithFormat:@"http://el3en.com/?articles=topic&topic=%@",token_ID];

           NSURL *articleURL = [NSURL URLWithString:articleString];
           NSError *error;
           NSString *articlePage = [NSString stringWithContentsOfURL:articleURL
                                                 encoding:NSASCIIStringEncoding
                                                 error:&error];

           [_webView loadHTMLString:articlePage baseURL:nil];

           //NSString *someHTML = [_webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByClassName('topic_content')[0].innerHTML;"];
           //NSLog(@"Content : %@",someHTML);
     }
     .....
}

- (void)webViewDidStartLoad:(UIWebView *)webView {
     [activityIndicator startAnimating];
     self.webView.hidden=true;
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
     [self.webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByClassName('topic_content')[0].sinnerHTML;"];

     [activityIndicator stopAnimating];
     self.webView.hidden=false;
}


谢谢

最佳答案

您应该使用innerHtml而不是sinnerHtml,除此之外,您还需要获取字符串并将此字符串加载到另一个Web视图中。请看例子

#import "ViewController.h"

@interface ViewController ()
@property (strong, nonatomic) IBOutlet UIWebView *fakeView;
@end

@implementation ViewController


UIActivityIndicatorView *activityIndicator;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    [activityIndicator stopAnimating];



    [self callDetailNews];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)callDetailNews{

    if ([[Reachability reachabilityForInternetConnection]currentReachabilityStatus]!=NotReachable)
    {
        // article link :: http://el3en.com/?articles=topic&topic=20840 (token_ID)

        // <td class="topic_content" colspan='6' itemprop="articleBody"  style="text-align:justify;  word-wrap: break-word; width:100%">

        // NSString *someHTML = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByClassName('box')[0].innerHTML;"];

        NSString *articleString = [NSString stringWithFormat:@"http://el3en.com/?articles=topic&topic=%@",@"20840"];

        NSURL *articleURL = [NSURL URLWithString:articleString];
        NSError *error;
        NSString *articlePage = [NSString stringWithContentsOfURL:articleURL
                                                         encoding:NSASCIIStringEncoding
                                                            error:&error];

        NSLog(@"Request %@",articleURL);
    self.fakeView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
        self.fakeView.delegate = self;
        [self.fakeView loadRequest:[NSURLRequest requestWithURL:articleURL]];

        //NSString *someHTML = [_webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByClassName('topic_content')[0].innerHTML;"];
        //NSLog(@"Content : %@",someHTML);
    }

}

- (void)webViewDidStartLoad:(UIWebView *)webView {
    [activityIndicator startAnimating];
    self.webView.hidden=true;
}

-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    NSLog(@"error %@" ,[error description]);
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {

    if(webView == self.fakeView) {
        NSString* javascriptString = [self.fakeView stringByEvaluatingJavaScriptFromString:@"document.getElementsByClassName('topic_content')[0].innerHTML"];

        NSLog(@"%@",javascriptString);
        [self.webView loadHTMLString:javascriptString baseURL:[NSURL URLWithString:@""]];
        self.fakeView = nil;
    }else{
        [activityIndicator stopAnimating];
        self.webView.hidden=false;
    }
}
@end

09-18 14:48