download pictures to the local node

Photo Download

One. demand

1. jsonFile brush elect imageUrl
2. Download images must be saved and uploaded jsonfiles with the same folder
3. The jsonfile Zhongyuan network address to jsonfile本地路径

two. analysis

1. Using node
2. Obtaining stored data datafile in the folder directory
3. Directory traversal, access to jsonthe file, the picture imgUrl
4. Download imgUrl, save to local directory
5. The jsondocument imgUrlwas changed to a local path

three. Code Decomposition

1. The introduction module

	let fs = require('fs');       //引入文件读取模块
	let request = require('request')

2. Gets datafile directory
Here Insert Picture Description

	let list = fs.readdirSync('./Data')
	console.log(list)       // [ 'diet.json', 'healthplan.json', 'materials.json' ]

3. Traversal list, read each jsonfile, access imgUrlpath

 list.forEach(file=>{
	// 可以通过 递归的方式获取imageUrl,
	// 也可以通过正则匹配,我这里主要介绍正则匹配
	  let contentText = fs.readFileSync(`./Data/${file}`,'utf-8');
      let arr =  contentText.match(/http[s]?:\/\/.+\.(jpg|gif|png)/g)
})

Taken only one jsondocument section:
Here Insert Picture Description
4. By getting to imageUrlbe downloaded to the local picture

   arr.forEach((url,idx)=>{
   		let filename =  url.split('/').pop()  // 已原网络图片的名称命名本地图片
                request({url}).pipe(
                        fs.createWriteStream(`./images/${filename}`).on('close',err=>{  console.log('写入失败',err) })
                )   
    })

Images are saved in the imagesfolder and file names use the original name saved
Here Insert Picture Description
5. I think the local jsonfile imageUrlnetwork address to the local address

Can be used as a regular address matches the picture, replacing the original address, you can use replace, but used before matchmatch had to save a match, I intend to Method 3 in the regular amended as

let arr = []
contentText = contentText.replace(/http[s]?:\/\/.+\.(jpg|gif|png)/g,res=>{
      let filename =  res.split('/').pop()
	arr.push(res)  // 匹配到的地址存于 arr中
	 return  `./images/${filename}`  // 同时将contentText 中的内容修改
})

// 将json 文件重新写入
fs.writeFile(`./Data/${file}`, contentText,res=>{
     console.log(res)
 })

four. Code

let fs = require('fs');//引入文件读取模块
let request = require('request')
let list = fs.readdirSync('./Data')

list.forEach(file => {
      let contentText = fs.readFileSync(`./Data/${file}`,'utf-8');

        let arr = []
       contentText =  contentText.replace(/http[s]?:\/\/.+\.(jpg|gif|png)/g,res=>{
                let filename =  res.split('/').pop()
                arr.push(res)
                return  `.images/${filename}`
       })
       
       arr.forEach((url,idx)=>{
                let filename =  url.split('/').pop()
                request({url}).pipe(
                        fs.createWriteStream(`./images/${filename}`).on('close',err=>{  
                                console.log('sdfsd',err) })
                )     
       })
      
       fs.writeFile(`./Data/${file}`, contentText,res=>{
                console.log(res)
        })
})

These are my personal summary, if you have a better way, please correct me, thank you!

Published 45 original articles · won praise 14 · views 30000 +

Guess you like

Origin blog.csdn.net/cmchenmei/article/details/99410995