从外部站点[Node.js]提取所有图像

从外部站点[Node.js]提取所有图像,node.js,httprequest,Node.js,Httprequest,我正在使用一个代码,该代码从一个网站获取所有图像,然后将这些图像作为字符串发送到浏览器,但不起作用 我试图使用http模块创建一个服务器,获取pinterest的主页,匹配所有图像标记,将每个匹配存储在一个数组中,最后发送它 代码如下: var http = require('http') , options = { host: 'www.pinterest.com' , port: 80 , path: '/' , method: 'GE

我正在使用一个代码,该代码从一个网站获取所有图像,然后将这些图像作为字符串发送到浏览器,但不起作用

我试图使用http模块创建一个服务器,获取pinterest的主页,匹配所有图像标记,将每个匹配存储在一个数组中,最后发送它

代码如下:

var http = require('http')
  , options = {
        host: 'www.pinterest.com'
      , port: 80
      , path: '/'
      , method: 'GET'
    }
  , images = [ ]
  ;


http.createServer( function ( request, response ) {

  http.request( options, function ( res ) {
    res.setEncoding( 'utf8' );
    res.on( 'data', function ( chunk ) {

      matches.push( chunk.match(/<img[^>]+src="([^">]+)/g) );

    });
  }).on('error', function(e) {
    console.log('problem with request: ' + e.message);
  });

  response.writeHead( 200, { 'Content-Type' : 'text/html' } );

  response.end( images.toString() );

}).listen(8888);
var http=require('http'))
,选项={
主持人:“www.pinterest.com”
,港口:80
,路径:'/'
,方法:'GET'
}
,images=[]
;
createServer(函数(请求、响应){
请求(选项、函数(res){
res.setEncoding('utf8');
res.on(‘数据’,函数(块){
matches.push(chunk.match(/]+)/g));
});
}).on('error',函数(e){
log('请求问题:'+e.message);
});
writeHead(200,{'Content-Type':'text/html'});
response.end(images.toString());
}).听(8888);
控制台中没有任何错误,但一分钟后,控制台会打印:


请求问题:套接字挂起

我认为您的正则表达式有问题。无论如何,此方法将为您提供以下数据:

var http = require('http')
  , options = {
    host: 'pinterest.com'
  , port: 80
  , path: '/'
  , method: 'GET'
}
  , images = [ ];

http.createServer( function ( request, response ) {


var req = http.get(options, function(res){
    res.setEncoding('utf8');
    res.on('data', function (chunk) {
        images.push( chunk.match(/<img[^>]+src="([^">]+)/g) );
    }).on('end', function(){
        response.writeHead( 200, { 'Content-Type' : 'text/javascript' } );
        response.end(images.toString());
    });
});

req.on('error', function(error){
    console.log('error: ' + error.message);
    response.writeHead( 200, { 'Content-Type' : 'text/html' } );
    response.end('error: ' + error.message);
});

}).listen(8888);
var http=require('http'))
,选项={
主持人:“pinterest.com”
,港口:80
,路径:'/'
,方法:'GET'
}
,图像=[];
createServer(函数(请求、响应){
var req=http.get(选项、函数(res){
res.setEncoding('utf8');
res.on('data',函数(块){
push(chunk.match(/]+)/g));
}).on('end',function(){
writeHead(200,{'Content-Type':'text/javascript'});
response.end(images.toString());
});
});
请求开启('error',函数(error){
console.log('error:'+error.message);
writeHead(200,{'Content-Type':'text/html'});
response.end('error:'+error.message);
});
}).听(8888);

我在这里使用了
http.get
方法,而不是
http.request

,即使您已经解决了问题,尝试使用该包也要容易得多。 这是我见过的最好的类似jQuery的Node包,非常完整

您可以加载远程HTML,然后过滤图像,例如

var imageUrl = $("img").attr("src");

此外,在
数据
事件中解析HTML可能会给您提供标记块,这是一个问题。

最终客户的请求是否得到满足?谢谢朋友,但是现在抛出一个带有以下错误的警告框:Windows脚本主机命令序列:C:\Program Files\nodejs\images\image.js行:1个字符:1个错误:对象例外代码:800A138F来源:Microsoft JScript运行时错误试一试这篇文章,哦,天哪!我试图在没有node命令的情况下运行代码;抱歉。@udidu,您如何从客户端调用此应用程序,您是否只需编写一些在该端口上调用此服务器的虚拟页??运行服务器后转到主机:8888,因为我们没有定义路由,只有根用户可以访问