Get Data on a webpage (picture, text, video)-B

Source: Internet
Author: User

Demo Address: http://download.csdn.net/detail/u012881779/8831835

Get all the pictures on the page, get all the HTML, get the page title, get the page content text ...

. h File Code:

//网页  //NSString *strPath = [NSString stringWithFormat:@"http://www.baidu.com/s?wd=%@&cl=3",theWord];  //视频  //NSString *strPath = [NSString stringWithFormat:@"http://www.itinge.com/music/16241.mp4"];  //图片  NSString *strPath = [NSString stringWithFormat:@"http://image.baidu.com/search/index?tn=baiduimage&istype=2&ie=utf-8&word=%@",theWord];  strPath = [strPath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

. m file Code:

@interface Viewcontroller () <uisearchbardelegate, uiwebviewdelegate,uigesturerecognizerdelegate>  @ Property (weak, nonatomic) Iboutlet Uisearchbar *searchbar;   @property (weak, nonatomic) Iboutlet UIWebView *webview;   @end   @implementation Viewcontroller  -(void) viewdidload {     [super viewdidload]; nbsp    _searchbar.delegate = self;      _webview.delegate = self;      [self Addtaponwebview];  }  -(void) addtaponwebview{     uitapgesturerecognizer* singletap = [[ UITapGestureRecognizer Alloc] initwithtarget:self action: @selector (Handlesingletap:)];      [_webview Addgesturerecognizer:singletap];      singletap.delegate = self;      singletap.cancelstouchesinview = NO;  }   #pragma mark-tapgesturerecognizer  -(BOOL) Gesturerecognizer: (Uigesturerecognizer *) Gesturerecognizer ShouldrecognizesimultaneouslywithgesturErecognizer: (Uigesturerecognizer *) othergesturerecognizer{     return YES;    //is clicked on the location corresponding to the link & nbsp;-(void) Handlesingletap: (UITapGestureRecognizer *) sender{     cgpoint pt = [Sender Locationinview: _webview];      nsstring *imgurl = [NSString stringwithformat:@ "Document.elementfrompoint (%f,%f). Src", pt.x, Pt.y] ;      nsstring *urltosave = [_webview Stringbyevaluatingjavascriptfromstring:imgurl];      nslog (@ "Image url=%@", urltosave);      nsstring * Jstogethtmlsource = @ "Top.location.href";      nsstring * Pagesource = [_webview   Stringbyevaluatingjavascriptfromstring:jstogethtmlsource] ;      nslog (@ "\n\n__url=%@", Pagesource);      if (Urltosave.length > 4) {         nsstring *substr = [Urltosave Substri NGFROMINDEX:URLTOSAVE.LENGTH-3];          if ([substr isequaltostring:@ "jpg"] | | [substr isequaltostring:@ "png") {             [self Showimageurl:urltosave point:pt];          }      }  }  //renders the image, whether the HTML adaptation resolution will affect whether the click resource is consistent with the obtained resource  -( void) Showimageurl: (nsstring *) URL point: (cgpoint) point  {     uiimageview *showview = [[ Uiimageview alloc] Initwithframe:[[uiscreen Mainscreen]bounds]];      showview.center = point;      cgpoint newpoint = Self.view.center;      showview.center = Newpoint;      showview.backgroundcolor = [Uicolor blackcolor];      showview.alpha = 1;      showview.userinteractionenabled = YES;      [self.view Addsubview:showview];      [showview Setcontentmode:uiviewcontentmodescaleaspectfit];      [showview Showimagefromurl:url Placeholder:nil Completionblock:nil];      uitapgesturerecognizer* SinglETap = [[UITapGestureRecognizer alloc] initwithtarget:self action: @selector (Handlesingleviewtap:)];      [showview Addgesturerecognizer:singletap];      [self.navigationcontroller Setnavigationbarhidden:yes Animated:yes];  }  //Remove Picture view view  -(void) Handlesingleviewtap: (UITapGestureRecognizer *) Sender  {     for (id obj in self.view.subviews) {         if ([obj Iskindofclass:[uiimageview class]) {             [obj Removefromsuperview],          }   &N Bsp &NBSP,}      [self.navigationcontroller setnavigationbarhidden:yes Animated:yes];  }  -(void) didreceivememorywarning {     [super didreceivememorywarning];      //Dispose of any of the resources that can recreated.   }  /* *javascript get the Web information summary get all html:nsstring *ljs = @ "Document.documentElement.innerHTML"; Get Web Title:nssTring *ljs2 = @ "Document.title";  UIWebView *lwebview = [self getcurrentwebview];  NSString *LHTML1 = [Lwebview Stringbyevaluatingjavascriptfromstring:ljs];  NSString *LHTML2 = [Lwebview stringbyevaluatingjavascriptfromstring:ljs2]; JavaScript gets Web page information summary JavaScript gets the current page URL, title, etc. thisurl = document.  URL;  Thishref = Document.location.href;  Thissloc = Self.location.href;  Thisdloc = document.location;  Thistloc = Top.location.href;  Thisploc = parent.document.location;  Thisthost = Top.location.hostname;  Thishost = Location.hostname;  Thistitle = Document.title;  Thisprotocol = Document.location.protocol;  Thisport = Document.location.port;  Thishash = Document.location.hash;  Thissearch = Document.location.search;  Thispathname = Document.location.pathname;  thishtml = Document.documentElement.innerHTML; Thisbodytext = document.documentelement.innertext;//Get page content text Thisbodytext = document.body.innertext;//Get page content text   How is it the same as last? If you know, please explain * *  //get  -(ibaction) receiveaction:(ID) Sender {     /*1 video */     /*    //Get all videos in the webpage     NSString *GETVI  Deotitle = [_webview getvideotitle];      NSLog (@ "\ n \ nthe video name:%@", getvideotitle);      Double getvideoduration = [_webview getvideoduration];      NSLog (@ "\ n \ nthe total video time:%f", getvideoduration);      Double getvideocurrenttime = [_webview getvideocurrenttime];      NSLog (@ "\ n \ nthe video current time:%f", getvideocurrenttime);     *      /*2 page */     /*    //get all the pictures in the web    nsstring *imag Eurls = [_webview stringbyevaluatingjavascriptfromstring:@ "var str=new Array ();" "  $ (' img '). each (function () {Str.push ($ (this). attr (' src '));}); "                            "Str.join (', ');"];     nslog (@ "\ n \ All pictures:%@", imageurls);    //get all html    nsstring *ljs = @ "Document.documentElement.innerHTML";     nsstring *LHTML1 = [_webview Stringbyevaluatingjavascriptfromstring:ljs];     //nslog (@ "1.%@", LHTML1);     //Get page title:    nsstring *ljs2 = @ "Document.title";     nsstring *LHTML2 = [_webview stringbyevaluatingjavascriptfromstring:ljs2];     nslog (@ "2.%@", LHTML2);    //thisurl = document. URL    nsstring *LJS3 = @ "document.  URL ";     nsstring *LHTML3 = [_webview stringbyevaluatingjavascriptfromstring:ljs3];     nslog (@ "3.%@", LHTML3);     //get Web content text    nsstring *ljs4 = @ "Document.documentElement.innerText";     nsstring *LHTML4 = [_webview stringbyevaluatingjavascriptfromstring:ljs4];     nslog (@ "4.%@", LHTML4);     //get Web content text    nsstring *ljs5 = @ "Document.body.innerText";     nsstring *LHTML5 = [_webview stringbyevaluatingjavascriptfromstring:ljs5];     nslog (@ "5.%@", LHTML5);    */  &nbsp  /*3 Pictures */     /**/     //get all HTML      nsstring *innerhtml = @ "Document . documentelement.innerhtml ";      nsstring *innerhtmlstring = [_webview stringbyevaluatingjavascriptfromstring:innerhtml];      //retrieving pictures      if (![ Innerhtmlstring isequaltostring:@ "

Article/author: Feng Zhi ought

C Blog Address: http://blog.csdn.net/wsyx768/article/details/46618125

Get Data on a webpage (picture, text, video)-B

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.