关于nodejs爬虫的问题?


var request = require('request');
var fs = require('fs');
var cheerio = require("cheerio");
var url = 'http://www.fssxhsd.com/category.php?id=332';

request(url,function(err,result){
    if(err){
        console.log(err);
    }
    var $ = cheerio.load(result.body);
    $('dl img').each(function(index,element){
        var img_src = 'www.fssxhsd.com/' + $(this).attr('src');
        console.log(img_src)
        //采用request模块,向服务器发起一次请求,获取图片资源
        request.head(img_src,function(err,res,body){
            if(err){
                console.log(err);
            }
        });
        request(img_src).pipe(fs.createWriteStream('./image/'+index));
    })
})

这是我的代码,但是报错:

Error: Invalid URI "www.fssxhsd.com/images/201703/thumb_img/277292_thumb_G_1489165224300.jpg"
    at Request.init (C:\Users\Administrator\Desktop\mystyle\forExample\reptile\node_modules\request\request.js:276:31)
    at new Request (C:\Users\Administrator\Desktop\mystyle\forExample\reptile\node_modules\request\request.js:130:8)
    at request (C:\Users\Administrator\Desktop\mystyle\forExample\reptile\node_modules\request\index.js:54:10)
    at Function.head (C:\Users\Administrator\Desktop\mystyle\forExample\reptile\node_modules\request\index.js:62:12)
    at Object.<anonymous> (C:\Users\Administrator\Desktop\mystyle\forExample\reptile\reptile2.js:16:17)
    at initialize.exports.each (C:\Users\Administrator\Desktop\mystyle\forExample\reptile\node_modules\cheerio\lib\api\traversing.js:300:24)
    at Request._callback (C:\Users\Administrator\Desktop\mystyle\forExample\reptile\reptile2.js:12:17)
    at Request.self.callback (C:\Users\Administrator\Desktop\mystyle\forExample\reptile\node_modules\request\request.js:188:22)
    at emitTwo (events.js:106:13)
    at Request.emit (events.js:194:7)
events.js:163
      throw er; // Unhandled 'error' event
      ^

Error: Invalid URI "www.fssxhsd.com/images/201703/thumb_img/277292_thumb_G_1489165224300.jpg"
    at Request.init (C:\Users\Administrator\Desktop\mystyle\forExample\reptile\node_modules\request\request.js:276:31)
    at new Request (C:\Users\Administrator\Desktop\mystyle\forExample\reptile\node_modules\request\request.js:130:8)
    at request (C:\Users\Administrator\Desktop\mystyle\forExample\reptile\node_modules\request\index.js:54:10)
    at Object.<anonymous> (C:\Users\Administrator\Desktop\mystyle\forExample\reptile\reptile2.js:21:9)
    at initialize.exports.each (C:\Users\Administrator\Desktop\mystyle\forExample\reptile\node_modules\cheerio\lib\api\traversing.js:300:24)
    at Request._callback (C:\Users\Administrator\Desktop\mystyle\forExample\reptile\reptile2.js:12:17)
    at Request.self.callback (C:\Users\Administrator\Desktop\mystyle\forExample\reptile\node_modules\request\request.js:188:22)
    at emitTwo (events.js:106:13)
    at Request.emit (events.js:194:7)
    at Request.<anonymous> (C:\Users\Administrator\Desktop\mystyle\forExample\reptile\node_modules\request\request.js:1171:10)

请问这是为什么啊,它这个报错说无效的url但是这个url是可以进入的啊?
谢谢!

阅读 2.5k
2 个回答
  var img_src = 'www.fssxhsd.com/' + $(this).attr('src');

加上 http或者https,谢谢

前面要加 http://

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏