Photo Download
One. demand
1. json
File brush elect imageUrl
2. Download images must be saved and uploaded json
files with the same folder
3. The json
file Zhongyuan network address to json
file本地路径
two. analysis
1. Using node
2. Obtaining stored data data
file in the folder directory
3. Directory traversal, access to json
the file, the picture imgUrl
4. Download imgUrl
, save to local directory
5. The json
document imgUrl
was changed to a local path
three. Code Decomposition
1. The introduction module
let fs = require('fs'); //引入文件读取模块
let request = require('request')
2. Gets data
file directory
let list = fs.readdirSync('./Data')
console.log(list) // [ 'diet.json', 'healthplan.json', 'materials.json' ]
3. Traversal list
, read each json
file, access imgUrl
path
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 json
document section:
4. By getting to imageUrl
be 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 images
folder and file names use the original name saved
5. I think the local json
file imageUrl
network 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 match
match 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!