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;
    }
    

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

    评论

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题的应用
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办