bluetooth99 2013-01-25 02:10 采纳率: 0%
浏览 3912

获取一个网页的全部<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 :

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

  • 写回答

4条回答 默认 最新

  • Chengzi_963 2013-01-25 02:57
    关注

    在加载的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;
    }
    

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

    评论

报告相同问题?

悬赏问题

  • ¥50 comsol稳态求解器 找不到解,奇异矩阵有1个空方程返回的解不收敛。没有返回所有参数步长;pid控制
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功