关于nodejs爬虫的问题?

发布于 2022-09-04 17:32:59 字数 3088 浏览 16 评论 0


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是可以进入的啊?
谢谢!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

说好的呢 2022-09-11 17:32:59
  var img_src = 'www.fssxhsd.com/' + $(this).attr('src');

加上 http或者https,谢谢

<逆流佳人身旁 2022-09-11 17:32:59

前面要加 http://

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文