Node JS抓取html字符串中的第一个图像

时间:2014-07-07 01:14:36

标签: javascript html node.js

我正试图抓住像这样的html字符串中的第一个图像

  <table border="0" cellpadding="2" cellspacing="7" style="vertical-align:top;"><tr><td width="80" align="center" valign="top"><font style="font-size:85%;font-family:arial,sans-serif"><a href="http://news.google.com/news/url?sa=t&amp;fd=R&amp;ct2=us&amp;usg=AFQjCNFfn6RXQ3v898sGY_-sFLGCJ4EV5Q&amp;clid=c3a7d30bb8a4878e06b80cf16b898331&amp;cid=52778551504048&amp;ei=zfK5U7D4JoLi1Ab0wIHwDw&amp;url=http://online.wsj.com/articles/obamas-letters-to-corinthian-1404684555"><img src="//t3.gstatic.com/images?q=tbn:ANd9GcQVyQsQJvKMgXHEX9riJuZKWav5U1nI-jdB-i1HwFYQ-7jGvGrbk9N_k0XEDMVH-HAbLxP1wrU" alt="" border="1" width="80" height="80" /><br /><font size="-2">Wall Street Journal</font></a></font></td><td valign="top" class="j"><font style="font-size:85%;font-family:arial,sans-serif"><br /><div style="padding-top:0.8em;"><img alt="" height="1" width="1" /></div><div class="lh"><a href="http://news.google.com/news/url?sa=t&amp;fd=R&amp;ct2=us&amp;usg=AFQjCNFfn6RXQ3v898sGY_-sFLGCJ4EV5Q&amp;clid=c3a7d30bb8a4878e06b80cf16b898331&amp;cid=52778551504048&amp;ei=zfK5U7D4JoLi1Ab0wIHwDw&amp;url=http://online.wsj.com/articles/obamas-letters-to-corinthian-1404684555"><b><b>Obama&#39;s</b> Letters to Corinthian</b></a><br /><font size="-1"><b><font color="#6f6f6f">Wall Street Journal</font></b></font><br /><font size="-1">The <b>Obama</b> Administration has targeted for-profit colleges as if they are enemy combatants. And now it has succeeded in putting out of business Santa Ana-based Corinthian Colleges for a dilatory response to document requests. Does the White House plan&nbsp;...</font><br /><font size="-1" class="p"></font><br /><font class="p" size="-1"><a class="p" href="http://news.google.com/news/more?ncl=dPkBozywrsIXKoM&amp;authuser=0&amp;ned=us"><nobr><b>and more&nbsp;&raquo;</b></nobr></a></font></div></font></td></tr></table>

这是图像的标签

<img src="//t3.gstatic.com/images?q=tbn:ANd9GcQVyQsQJvKMgXHEX9riJuZKWav5U1nI-jdB-i1HwFYQ-7jGvGrbk9N_k0XEDMVH-HAbLxP1wrU" alt="" border="1" width="80" height="80">

每张图片都有这种网址 //tx.gstatic.com 其中x是我认为在0<x<3

之间的数字

这就是我没有成功的事情,我不明白为什么会发生这种情况

      var re = /<img[^>]+src="?([^"\s]+)"?\s*\/>/g;
      var results = re.exec(HTMLSTRING);
      var img="";
      if(results!=null && results.length!=0) img = results[0];

2 个答案:

答案 0 :(得分:7)

您提供的正则表达式确实不足以捕获您的<img>标记。

有两种选择:

  • 制作更好的正则表达式。这种方式就是疯狂。但在这种情况下,在src之后添加其他属性的可能性就足够了:

    var re = /<img[^>]+src="?([^"\s]+)"?[^>]*\/>/g;
    var results = re.exec(HTMLSTRING);
    var img="";
    if(results) img = results[1];
    

    注意[^>]*替换\s*,如果您想要源而不是标记本身,请注意results[1]而不是results[0]

  • 使用DOM解析器来处理DOM。这是一条简单的道路。

    var jsdom = require("jsdom");
    var img_sources = jsdom.env(
      HTMLSTRING,
      function (errors, window) {
        var imgs = window.document.getElementsByTagName('img');
        for (var i = 0; i < imgs.length; i++) {
          var src = imgs[i].getAttribute('src');
          if (src) console.log(src);
        }
      }
    );
    

答案 1 :(得分:0)

您可以使用jQuery NPM module并执行此操作:

var jQuery = require('jquery');

try {
    var src = jQuery('YOUR_HTML_STRING').find('img')[0].src;
    console.log('Output:\nSrc: ' + src + '\nNum: ' + (src.match(/\/\/t[0-3]/)[0])[3]);
} catch (e) {
    console.log('Could not find <img>!');
}