1

我正在尝试获取给定网页的图像。我尝试过使用这些模块requestjsdom如下所示:

var request = require('request'),
    jsdom = require('jsdom');

request({ uri:'http://www.google.com' }, function (error, response, body) {
  if (error && response.statusCode !== 200) {
    console.log('Error when contacting google.com')
  }

  jsdom.env({
    html: body,
    scripts: [
      'http://code.jquery.com/jquery-1.8.2.min.js'
    ]
  }, function (err, window) {
    var $ = window.jQuery;

    $(function () {
      console.log($('img').length);
    })
  });
});

不幸的是,尽管页面包含图像,但console.log($('img').length);打印...0google.com

我究竟做错了什么?

4

1 回答 1

0

您可以在服务器中使用Cheerio jquery 实现。

这是从站点读取图像 URL 的 Coffee 脚本要点

https://gist.github.com/mrjjwright/3240020

Javascript 版本

https://gist.github.com/fizerkhan/5633503

于 2013-05-23T08:19:17.347 回答