网页去广告

网页去广告,第1张

概述网页去广告

下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。

内存溢出小编现在分享给大家,也给大家做个参考。

webVIEw加载网页的时候,有的时候会有广告,去除网页中的广告
// 方法一: - (voID)showNews:(NSDictionary *)news { // 1.取出网页内容 Nsstring *body = news[@"body"]; // 2.取出图片 NSDictionary *img = [news[@"img"] lastObject]; Nsstring *imgHTML = [Nsstring stringWithFormat:@"<img src=\"%@\" wIDth=\"300\" height=\"171\">",img[@"src"]]; // 2.创建一个webVIEw,显示网页 UIWebVIEw *webVIEw = [[UIWebVIEw alloc] init]; webVIEw.frame = self.vIEw.bounds; [self.vIEw addSubvIEw:webVIEw]; // 3.拼接网页内容 Nsstring *HTML = [body stringByReplacingOccurrencesOfString:img[@"ref"] withString:imgHTML]; // 4.取出新闻标题 Nsstring *Title = news[@"Title"]; // 5.取出新闻的时间 Nsstring *time = news[@"ptime"]; // 头部内容 Nsstring *header = [Nsstring stringWithFormat:@"<div class=\"Title\">%@</div><div class=\"time\">%@</div>",Title,time]; HTML = [Nsstring stringWithFormat:@"%@%@",header,HTML]; // 链接mainBundle中的CSS文件 NSURL *CSSURL = [[NSBundle mainBundle] URLForResource:@"news" withExtension:@"CSS"]; HTML = [Nsstring stringWithFormat:@"%@<link rel=\"stylesheet\" href=\"%@\">",HTML,CSSURL]; // 5.加载网页内容 [webVIEw loadHTMLString:HTML baseURL:nil]; }// 方法二:火狐浏览器:在要去掉的广告上,右键”查看元素“,将代码粘贴到要替换的字符串处 Nsstring *url = @"http://ibaby.ipadown.com/API/food/food.show.detail.PHP?ID=";    Nsstring *webSite = [url stringByAppendingString:self.IDtext];    NSURL *siteURL= [NSURL URLWithString:webSite];    NSURLRequest *request = [NSURLRequest requestWithURL:siteURL];    // 加载请求//    [self.webVIEw.web loadRequest:request];    dispatch_async(dispatch_get_global_queue(0,0),^{        NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession]dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data,NSURLResponse * _Nullable response,NSError * _Nullable error) {            self.HTMLAbosluteString = [[Nsstring alloc]initWithData:data enCoding:NSUTF8StringEnCoding];            self.HTMLAbosluteString = [self.HTMLAbosluteString stringByReplacingOccurrencesOfString:@"<img wIDth=\"0\" height=\"0\" style=\"wIDth: 803px; height: 121px;\" src=\"http://ubmcmm.baIDustatic.com/media/v1/0f0007y3CMO4YsUwoxXbB6.jpg\"></img>" withString:@""];// 这是要去掉广告的第二个地方self.HTMLAbosluteString = [self.HTMLAbosluteString stringByReplacingOccurrencesOfString:str9 withString:@""];            dispatch_async(dispatch_get_main_queue(),^{                [self.webVIEw.web loadHTMLString:self.HTMLAbosluteString baseURL:nil];            });        }];        [dataTask resume];        CGRect tmpFrame = self.webVIEw.web.frame;        CGfloat h = self.navigationController.navigationbar.frame.size.height + 20;        tmpFrame.origin.y -= h;        self.webVIEw.web.frame = tmpFrame;    });// 方法三: Nsstring *str = @".jpg\" height=\"200\" wIDth=\"340\"/>";- (Nsstring *)transformMessage:(Nsstring *)message withString:(Nsstring *)string{    Nsstring *messagecopy = message;    NSScanner *theScanner;    Nsstring *text = nil;    theScanner = [NSScanner scannerWithString:messagecopy];    while ([theScanner isAtEnd] == NO)    {         // find start of tag        [theScanner scanUpToString:@".jpg" intoString:NulL] ;         // find end of tag        if ([theScanner scanUpToString:@">" intoString:&text] || [theScanner scanUpToString:@"/>" intoString:&text]) {            // .jpg" height="200" wIDth="340"/>            // 如果不需要第二个参数 :            // 可以将string替换为 [Nsstring stringWithFormat:@".jpg\" height=\\"200\" wIDth=\"340\"/>"]            if (text != nil) {                messagecopy = [messagecopy stringByReplacingOccurrencesOfString:[Nsstring stringWithFormat:@"%@>",text] withString:string];                NSLog(@"%@",messagecopy);                break;            }        }    }    return messagecopy;}作用是把start tag和end tag 之间替换成string的值

以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

总结

以上是内存溢出为你收集整理的网页去广告全部内容,希望文章能够帮你解决网页去广告所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1105818.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存