问题

在使用Node.js写爬虫的时候,使用到了node 的request库,请求图片URL并保存到本地时。

遇到报错 Unhandled stream error in pipe

request(downloadURL).pipe(fs.createWriteStream(path))

NodeJS - Unhandled stream error in pipe报错

原因

根据错误提示的关键词查询。发现时node写入文件时,打开pipe Stream流进行文件处理后,没有正确关闭pipe管道,导致的报错

解决方案

查询百度,找到了一个非常好的解决方法

request的pipe方法的问题

根据问题回答,就可以解决了。

代码

const fs=require('fs');
const request=require('request');
// autoClose:true 写入完成后自动关闭管道
let fileStream=fs.createWriteStream('./1.jpg',{autoClose:true})
request(downloadURL).pipe(fileStream);
// 完成写入操作后,进行提示。
fileStream.on('finish',function(){
    console.log('文件写入成功')
})

相关文章:

  • 2022-12-23
  • 2021-09-21
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-10
  • 2022-02-06
猜你喜欢
  • 2022-12-23
  • 2021-12-24
  • 2021-11-02
  • 2022-12-23
  • 2022-02-06
  • 2021-07-26
  • 2022-01-10
相关资源
相似解决方案