美文网首页
ios解析html内容,获取想要的标签

ios解析html内容,获取想要的标签

作者: Mr_LS | 来源:发表于2020-05-13 10:46 被阅读0次

1.最近项目上架苹果商店用到了webview,被拒了,原因如图:

截屏2020-05-1310.20.00.png

其实在项目中是做了判断的,ios8以后用的wkwebview,而ios8以前采用webview,还是被拒,果断删除了这个判断,因为项目本身就已经要求了环境是ios9以上,然后重新提交,继续被拒,原因还是同样的原因,然后就搜整个项目的webview,在三方请求afnet里面有个webview的类别,继续删除这个类别,重新提交才ok

ps: afnet不是最新的,因为是pod进项目的,没有及时更新,其实更新一下一样ok

2.项目中用到webview的需求如下:

拿到地址url,但这个地址直接打开是有广告等一些不要展示在项目中的,所以就需要解析里面的标签对,获取想要展示的标签

项目中用的一个三方库hpple

先用wkwebview加载该url

  NSString *jScript = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);";
    //        document.getElementsByTagName('img').style.maxWidth='100%';
    WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
    WKUserContentController *wkUController = [[WKUserContentController alloc] init];
    [wkUController addUserScript:wkUScript];
    WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init];
            wkWebConfig.userContentController = wkUController;
    _loadWebView=[[WKWebView alloc] initWithFrame:self.view.bounds configuration:wkWebConfig];
    _loadWebView.backgroundColor = [UIColor whiteColor];
    _loadWebView.navigationDelegate = self;
NSURLRequest *request = [NSURLRequest requestWithURL:_homeUrl];
        [wkWebView loadRequest:request];

然后在代理方法中解析出想要的内容,直接上代码:

    - (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(null_unspecified WKNavigation *)navigation{
NSString *urlString=webView.URL.absoluteString;
if ([ webView.URL.absoluteString containsString:@"https://mini.eastday.com"]) {
    if ([urlString hasPrefix:@"https://mini.eastday.com/mobile"]) {
        NSString *last=[urlString componentsSeparatedByString:@"/mobile/"].lastObject;
        urlString=[NSString stringWithFormat:@"https://mini.eastday.com/a/%@",last];
    }
    NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:urlString]];
    TFHpple * doc       = [[TFHpple alloc] initWithHTMLData:data];
    NSArray * elements  = [doc searchWithXPathQuery:@"//div[@class='J-contain_detail_cnt contain_detail_cnt']"];
    NSString *js=@"<script type=\"text/javascript\">window.onload= function(){\
                var oImg = document.getElementsByTagName(\"img\");\
                for(var i=0;i<oImg.length;i++){\
                        oImg[i].removeAttribute(\"height\");\
                }\
            }</script>";
    NSString *head1=@"<html><head>";
    NSString *head2=@"<title></title></head><style>img{max-width: 100% !important;}p{\"text-indent:2em;font-size: 30px;\"}</style><body>";
    NSString *headStr=[NSString stringWithFormat:@"%@%@%@",head1,js,head2];
    NSString *last=@"</body></html>";
    
    for (int i = 0; i < elements.count; i++){
        TFHppleElement *ulE = [elements objectAtIndex:i];
        NSString *rrr=[ulE.raw stringByReplacingOccurrencesOfString:@"src=\"" withString:@"src=\"https:"];
        NSString *uuu=[NSString stringWithFormat:@"%@%@%@",headStr,rrr,last];
        [self.loadWebView loadHTMLString:uuu baseURL:nil];
        [self.view addSubview:self.loadWebView];
    }
}else{
}

}

ps:webview中注入了js是因为获取到的图片大小没有适配手机屏幕

相关文章

网友评论

      本文标题:ios解析html内容,获取想要的标签

      本文链接:https://www.haomeiwen.com/subject/yfvbwctx.html