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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog