开发者社区> 问答> 正文

获取一个网页的全部<image src>

我需要获取当前页面UIWebView中的全部图片url。
代码如下:

- (void)webViewDidFinishLoad:(UIWebView*)webView {
    NSString *firstImageUrl = [self.webView stringByEvaluatingJavaScriptFromString:@"var images = document.getElementsByTagName('img');images[0].src.toString();"];
    NSString *imageUrls = [self.webView stringByEvaluatingJavaScriptFromString:@"var images= document.getElementsByTagName('img');var imageUrls = "";for(var i = 0; i < images.length; i++){var image = images[i];imageUrls += image.src;imageUrls += \\’,\\’;}imageUrls.toString();"];
    NSLog(@"firstUrl : %@", firstImageUrl);
    NSLog(@"images : %@",imageUrls);
}
第一个NSLog返回了正确的结果,但是第二个NSLog返回空:
2013-01-25 00:51:23.253 WebDemo[3416:907] firstUrl: https://www.paypalobjects.com/en_US/i/scr/pixel.gif
2013-01-25 00:51:23.254 WebDemo[3416:907] images :

不知道问题出在哪儿了,请高手指点。

展开
收起
小旋风柴进 2016-03-17 10:05:22 1959 0
1 条回答
写回答
取消 提交回答
  • 在加载的webview html源码中运行一个正则表达式来实现:

    (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    {
      static NSString *CellId = @"Cell";
    
      Cell *cell=[table dequeueReusableCellWithIdentifier:CellId];
      if (cell==nil) {
          [[NSBundle mainBundle]loadNibNamed:@"Cell" owner:self options:nil];
          cell=self.custom;
      }
      // Get the event corresponding to the current index path and configure the table view cell.
      Student *stu = (Student *)[dataarr objectAtIndex:indexPath.row];
      cell.item.text = [stu name];
      cell.rate.text = [stu rate];
      currentindex=indexPath.row;
      return cell;
    }

    这是个基本的正则表达式,需要根据图片属性添加一些别的细节。

    2019-07-17 19:04:35
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
<img src...[mitah].1536575523.pdf 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载