问题描述
我正在尝试从网站上抓取数据,但是我从服务器目录中打开图像时却收到文件加载图像时发生错误之类的错误。图像存储有扩展名,但图像无法正确打开
var request = require('request');
var cheerio = require(’cheerio’);
const fs = require( fs);
函数hello(){
url =’’;
request(url,function(error,response,html){
if(!error){
var $ = cheerio.load(html);
var img = $('img.control-label');
var img_url = $('img.control-label')。attr('src')
console.log(img );
console.log(url + img_url);
request({url:url + img_url,headers:{Cookie:response.headers,encoding:null}},function(error1 ,response1,body){
if(!error){
path1 ='./image/new.jpeg';
data = data: + response.headers [ content- type] +; base64, + new Buffer(body).toString('base64');
console.log(data);
fs.writeFile(path1,body,{encoding:' base64'},function(err){
});
}
})
}
})
}
hello();
以下是写入文件之前数据的控制台日志的结果
'pre>
数据:text / html的; base64,77 + 977 + 977 + 977 + 9ABBKRklGAAEBAAABAAEAAO + / VE + / vQA7Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJSkcgSlBFRyB2OTApLCBxdWFsaXR5ID0gODAK77 + 977 + 9AEMABgQFBgUEBgYFBgcHBggKEAoKCQkKFA4PDBAXFBgYFxQWFhodJR8aGyMcFhYgLCAjJicpKikZHy0wLSgwJSgpKO + / VE + / vQBDAQcHBwoIChMKChMoGhYaKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCjvv73vv70AEQgAGQBBAwEiAAIRAQMRAe + / VE + / vQAfAAABBQEBAQEBAQAAAAAAAAAAAQIDBAUGBwgJCgvvv73vv70A77 + 9EAACAQMDAgQDBQUEBAAAAX0BAgMABBEFEiExQQYTUWEHInEUMu + / VE + / VE + / + vQgjQu / VE + / + vRVS77 977 + 9JDNicu + / vQkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXrvv73vv73vv73vv73vv73vv73vv73vv73vv73vv73vv73vv73vv73vv73vv73vv73vv73vv73vv73vv73vv73vv73vv73vv73vv73vv73vv73vv73vv73vv73vv73vv73vv73vv73vv73vv73vv73vv73vv73vv73vv73vv73vv73vv73vv73vv73vv73vv73vv73vv73vv73vv73vv73vv73vv73vv73vv73vv73vv73vv73vv73vv73vv73vv73vv73vv73vv73vv7 3vv73vv73vv73vv73vv73vv73vv70AHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL77 + 977 + 9AO + / vREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMu + / vQgUQu + / VE + / VE + / VE + / vQkjM1Lvv70VYnLvv70KFiQ077 + 9Je + / vRcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl677 + 977 + 977 + 977 + 977 + 977 + 977 + 977 + 977 + 977 + 977 + 977 + 977 + 977 + 977 + 977 + 977 + 977 + 977 + 977 + 977 + 977 + 977 + 977 + 977 + 977 + 977 + 977 + 977 + 977 + 977 + 977 + 977 + 977 + 977 + 977 + 977 + 977 + 977 + 977 + 977 + 977 + 977 + 977 + 977 + 977 + 977 + 977 + 977 + 977 + 977 + 977 + 977 + 977 + 977 + 977 + 977 + 977 + 977 + 977 + 977 + 977 + 977 + 977 + 977 + 977 + 977 + 977 + 977 + 977 + 977 + 977 + 977 + 977 + 9AAwDAQACEQMRAD8A77 + 9Uu + / ve + / vTwL77 + 977 + 9aF4k77 + 977 + 90bTvv71tQ1c / 77 + 977 + 9We + / vWnvv70p2IDvv71FGDHPVO + / + / ve + 977 + 9e2g177 + 9PDHvv71 / GHjvv73vv71PGFvvv71I2o / vv71v77 + 9AU0u3Ksx77 + 9ee + / VE + / vQBI77 + 9W3Nl77 + 9CO + / VE + / vWDvv70AHh9FfSHvv73vv73vv73vv73vv73vv73vv70 +胡+ / VE + / vS07Se + / vUxtbO + / vW7vv73vv71777 + 9W3Pvv73bgu + / VE + / VE + / vTM777 + 9AO + / vUwN77 + 9FzXvv73vv71 + IO + / vXxx77 + 977 + 977 + 9Ke + / ve + / ve + / vSbvv71W77 + 9anJp77 + 977 + 977 + 9b2Mccjfvv711au + / ve + / vRFdf3Y877 + 9w7Dvv7 0oAWgD77 + 96K + OP,++ / VWX / 77 + 9Yn /酒庄+ / vRXvv73vv70q77 + 9AO + / VE + / VE + / VR / vv715S ++ / vR9m77 + 9P ++ / vT7VjG7vv70 / d ++ / vWfvv73vv73vv71t77 + 9FH7vv73vv70O77 + 977 + 977 + 9ABN4Wu + / VE + / vW0TWda1aGTvv73vv73vv71qVuW0uO + / vQ7vv70EUG / vv71ackLvv73vv73vv70I77 + 9Ue + / vV3vv70D77 + 977 + 9Kmvvv71377 + 977 + 977 + 977 + 9UxNJC ++ / vWxi77 + 9ZEJB77 + 977 + 977 + 9SU + / VE + / vSDvv71677 + 9UO + / vQFFFFAG34Q8Ue + / VE + / vT9ZXVvvv73vv70RWu + / vSjvv70aTO + / VE + / vUxQNwdo77 + 9WCkj77 + 977 + 9HBIz77 + 9cmt + Je + / ve + / vW1S77 + 977 + 9DSI477 + 9xIDvv73vv70277 + 977 + 977 + 9CO + / ve + / vVjvv71PBO + / ve + / vQLvv70o77 + 9De + / vU8Va3Z6BDotv / v + v + vvv / v + V + V + V / v + V + V + V / V + V + V + V / v + V + V + V / V + V + V + V / V + V + V + V / V + V / V + V + V + V / V + V + V + V / V + V + V + V / V + V + V + V vT1y77 + 977 + 977 + 977 + 977 + 977 + 9 + ve + / ve + / vWrvv73vv73vv71D77 + 9eFdd77 + 977 + 9dBvvvv73vv71qVtu877 + 977 + 977 + 977 + 977 + 9bu + / vSjvv70rAg / vv73vv71yO ++ / vU0UAFFFFABRRRQB77 + 977 + 9
待处理(结点:)安全和d可用性问题。请改用Buffer.alloc(),Buffer.allocUnsafe()或Buffer.from()方法。
编辑1:
经过一番尝试,我得出以下结论:当写入以下图像时,图像实际上已转换为某种不同的格式:代码1与我的硬编码uri一起工作,但是当我将相同的代码与URL转换时,会出现异常错误
// console.log(data);不破坏
binaryData1 = data:image / jpeg; base64, + body.toString();
base64Data1 = binaryData1.replace(/ ^ data:image\ / jpeg; base64,/,);
binaryData1 =新的Buffer(base64Data1,'base64')。toString('binary');
的console.log(主体)
//工作
binaryData =数据:图像/ JPEG; BASE64,/ 9J / 4AAQSkZJRgABAQAAAQABAAD / 2wCEAAkGBxMTEhUTExMVFhUXGBcYFxcYGBgXGBgXGhcXFxcaFxcYHSggGholHxcYITEhJSkrLi4uHR8zODMtNygtLisBCgoKBQUFDgUFDisZExkrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrK // AABEIAQwAvAMBIgACEQEDEQH / xAAbAAACAwEBAQAAAAAAAAAAAAAEBQIDBgcBAP / EADoQAAEDAgUCAwYGAQQBBQAAAAEAAhEDIQQFEjFBUWEicYEGEzKRodEUQlKxwfAjBzPh8WIVQ4KSsv / EABQBAQAAAAAAAAAAAAAAAAAAAAD / xAAUEQEAAAAAAAAAAAAAAAAAAAAA / 9oADAMBAAIRAxEAPwBK + kqXUU0NNVmkgVuoqDaEkBNDRXtCj42lBfg / ZR1Rs6gD0IP7ypu9iq / BYfUj + FsMtxcNu0IqtmrWj4T8wg55U9lsQPyT5EKh2QVh / WC0 / WCU / sugjO6fLXD0 + yIp5zQ / VHmCP3CDl1TLnjdpHmCFUcKuw0sVRds9h9Ql + ZYOk8wQ2PRByz8OVD3JXW8L7M0HN8VNvy / LQR + xWGOzSPJx / koOTgHqvveOXSq3sFSOz3j5H + EUR + wTvy1B6t + xQYcViETRqSnz / YnETbSR5kKp / spimXNOe4c0 / wAoEdUlU + 8KYV6JaYcII3CHLUA5fZI8ebp7WFln8Z8SAQhRIU3KBKCBC8XpXkoOqwvCxXNavQ1AP7tVVWXTANVLWS9AZgA9osUS2TuiqFMBqqG6C04KUvxeH08J7QqCEJmIBC BZQc3mE2wlJpCzjqoBTrB4xoAQNW13M2cY8z9EUcxdFn / sk7sU07FC4it0QMaueVQbFpHcfYhW08 + dHiYPQx91nHTK9dUd6INZhs9by0j5FFVs3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VY3Gt6rGsxjj1RFB7iQg2WEqAvb5roOAA0hcxyxhJHJW9y4PDQgC9tyBQf5FcYyLLTWqz3 / AGXT / byq80iOqxXsQ8Sezig6hkOWNY0W4VuY4ZvRSw + YBrdkpxub6ngINBgqIDQi2vb2SUY0abJWMcZN0HvtXiNPjLQabZJE9oBMcSAfksv7L + xnvKfv8QA5p / 2qZA2n43 / qngevRap + HFbciADIP5hvp9VTXzxxsxukbW4A47IFODySpQqve + o0UdWsU9zrGxAHw9 / IJ1l + f0a0ta4am2I2KS5qHGm5xJMCfNZ5 + XjRTxFF + l7hx + ofE1w6jZBv8Tieiwvtm5huWibozCZ2ajSHDS9phwWU9rsYS4DsgQ4is7aYHZBo33fhaev3VFRsfQoBwvnNVhAVVRyCJRNOvtMkCPvH0QXKJY7gDgyg1eGqBwEbQrCEtwOMaGAeneUypEEAoPsOwJthKeyT0Kl01wVS6DYezFCX3XRqWHAaudezNbS663oxg07oMh7dkBsLMf6eZcH6njYuP7q // UXHk + EHqmP + nrmsptag2ZyoRtws3isq / wAgjqtn + KbBulAcHVEFQyiWpbWyMg2K2tMCEuzxsUi5v5bnyvP7z6IOfZ5jjRcGtNmktd31R4vKRHYFWfhHPjS7wm58ugXmdYZtW / z8l5lrnU2BszFpQN62CDqelc / xPs5XpvdpJLCdRB69luG5lCqxOZgjhBh20nNeXXuLjuFnc + 8T4WxzrHsAJWAx + ILnzwgjiajYa1kwAd + bmf4QtSpIH1RDcPqFkPWoubughqmypcVYHqDm / N B4wqykSOEWaDGsF5cd + g7dyqtEcoGWCpsjYuPQfytE1tkpyXARFQuJJG3H / abAoFdF6b5Y + 6RUnJxlW6DbZLuCtFXqnSsvlU8JrXqvhBk / aPDue / cpx7O0HtEBfPpkmSEZg6ulA1dXqBq + yis4vkyh6 + YCFZk + LbKDVCsYXoxPVVMxTTyvnPb2QI8y9nwZdQdoP6HfB6EXb9R5LMYl7qTtNRpae + x8jsVvKsQsp7UUtTbIM / icYOCkWOxxH5irq1FyS4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BKq6WuBE2dbrboSB + yxtYDwyRqNmwDO0k + lytFm1QFkESDbc9DGw6xv / AMpS6gCRe94O8HcQNzaDIB3QB0XgtAIMkapi1rT5ySqcRhw5gJkBrnSeNQvIBNoHPlPdiKBdRJYW3NnnUC0X3G + wNwOD1svq0D7ym0kahE73OqD2vfeLDhBHDv8ADchxdc6bGQCAfO4 / 5Qb8UP8AI0fF3EbEWF + O / flMsVShsGWi8gHn5dhsPrJKmoARE6TA4 + Yt579PQIPLQZ5m4NuYhC1nX / KLnYj6X22 / 5XzvCIF7T0nmyg1xgkjnfbjn + 8IJspyQ2YnvbaRMb8LX5fVIa0ON4CyF2wRud + 4PbgR809xGLgA8oNLSqIynWWey7HBw7prTqIGWuQk2bYaRP98kwpVVDEtnyQZivTIQ + pMcYIkFKXNug9rVkL7xWVhZCoNw56kyoqKNFxBIBIBAkd5P8KxlB43aY3246 + VjdA1yymCSS5lmlwBmJkATYzG8X2804ZTc40w97XNeACCXeIi5cLWIDh8uiQUtTDqggt7bcXnzAg 9UbVxzy0CGCx + EeIAx / wDUERtwgGxbdJgODrbtmPqBdCg9USAbeF1yIsbyDEfJR9y430u + R2QIs2xWhwdvEDT33FufJFYPB6yHhoHxSYkCbWG089voqH0i + uN27EWiQPzf0fpWjw7Q0ACw4QDU8vaOXfOOnTyHyQWYZXqOoRIEfCBxG42lPwVRWegxGPpv1OboiG25aTbpfiUoLyGkvI1aoFhYff0sIW0zWHA + RWNxFER8N + TFV + DsgHq3Mx1 ++ 3kqgCTOo3 / aIPnyP7Csqt + cR91AumwIAjvHM7fwg + dVm8m3z6IgVS6EM2nsZ4PpF / 2RuV4N9R0AGAbkoGODoFjZ / MB + nRPcC4kT + 6twuCaN7lEGAggTCmavCqqOCq96EAmZMskryn2JdOyR1hdBRUVBar33VJQa2hiajbNcQJntNuPRF4bF1TDA83sOO2 / Czbcwd0b9fuimZi4HZvyP3QajTVcNJcSIuJFr8gcyAovNYeEW5tpnYAeZtY7rPNzN1 / C36 / dTOaviIbv0P3QPalXEsBEn807Hzm3RAYjM6jWnxQACB4RYHja4mLdktdmr5JhvPB535QmY5g5zHSG8Hngg9UFH / rTvfOe4z + UHsD2 / sQnmEz1jvzQsHUrGBYbKptczwg6g3MWkbhU1sxHVc4di3jZxXjsfU / Ug2eKx7eoWexlUONjab2niQNxaQfLvsk7sS47lWYSsQTt63QGVduv7Xtx5qgN2DZJPESV9QfJFgATsJt5SVosE4MHhY3zMk / OUFeUZI4w6pYfp + / RaOlSbTEAADtZKH5m + dm / I / dDVszfPH1 + 6B + / FIepjFnzmL + 31 + 6pfjndvr90DypjJVL8ZCRnGO7KQxB6D6oGzcWhazryhPxR6D6 / deVcSeg + v3QEF6rKC / EHsvffnsg // 2Q =;
base64Data = binaryData.replace(/ ^ data:image\ / jpeg; base64,/,);
binaryData =新的Buffer(base64Data,'base64')。toString('binary');
console.log(binaryData)
fs.writeFile( out1.jpg,binaryData, binary,函数(err){//工作
if(err)
console.log( ERROR: + err); //写出没有错误的文件,但这不是有效的图像
});
fs.writeFile( out2.jpg,body, binary,function(err){//不起作用
if(err)
console.log( ERROR: + err); //正确地写出文件,但是它不是有效的图像
});
替换
fs.writeFile(path1,body,{encoding:'base64'},function(err){
});
有
fs.writeFile(path1,'data:image / jpeg; base64',+ new Buffer(body).toString('base64'),{encoding:'base64'},function(err){
});
I'm trying to scrap data from website while scrapping it i'm getting error like file "An error ocured while loading image" while opening image from my server directory.image is stored with extension but the image is not opening properly
var request = require('request');
var cheerio = require('cheerio');
const fs = require("fs");
function hello (){
url = '';
request(url, function(error, response, html){
if(!error){
var $ = cheerio.load(html);
var img = $('img.control-label');
var img_url = $('img.control-label').attr('src')
console.log(img);
console.log(url+img_url);
request({url: url+img_url, headers: {Cookie: response.headers,encoding:null }}, function(error1, response1, body){
if(!error){
path1 = './image/new.jpeg';
data = "data:" + response.headers["content-type"] + ";base64," + new Buffer(body).toString('base64');
console.log(data);
fs.writeFile(path1, body, {encoding: 'base64'}, function(err){
});
}
})
}
})
}
hello();
below is the result of the console log of data before writing the file
EDIT 1:
after some tries I've come to this that image actually converted to some diffrent format when to write following are the code 1 is working with my hard coded encoded uri but when i convert same code with URL it's giving the unusual errors
//console.log(data); not wrking
binaryData1 = "data:image/jpeg;base64,"+body.toString();
base64Data1 = binaryData1.replace(/^data:image\/jpeg;base64,/, "");
binaryData1 = new Buffer(base64Data1, 'base64').toString('binary');
console.log(body)
//working
binaryData = "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxMTEhUTExMVFhUXGBcYFxcYGBgXGBgXGhcXFxcaFxcYHSggGholHxcYITEhJSkrLi4uHR8zODMtNygtLisBCgoKBQUFDgUFDisZExkrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrK//AABEIAQwAvAMBIgACEQEDEQH/xAAbAAACAwEBAQAAAAAAAAAAAAAEBQIDBgcBAP/EADoQAAEDAgUCAwYGAQQBBQAAAAEAAhEDIQQFEjFBUWEicYEGEzKRodEUQlKxwfAjBzPh8WIVQ4KSsv/EABQBAQAAAAAAAAAAAAAAAAAAAAD/xAAUEQEAAAAAAAAAAAAAAAAAAAAA/9oADAMBAAIRAxEAPwBK+kqXUU0NNVmkgVuoqDaEkBNDRXtCj42lBfg/ZR1Rs6gD0IP7ypu9iq/BYfUj+FsMtxcNu0IqtmrWj4T8wg55U9lsQPyT5EKh2QVh/wC0/wCU/sugjO6fLXD0+yIp5zQ/VHmCP3CDl1TLnjdpHmCFUcKuw0sVRds9h9Ql+ZYOk8wQ2PRByz8OVD3JXW8L7M0HN8VNvy/lQr+xWGOzSPJx/koOTgHqvveOXSq3sFSOz3j5H+Eur+wTvy1B6t+xQYcViETRqSnz/YnETbSR5kKp/spimXNOe4c0/wAoEdUlU+8KYV6JaYcII3CHLUA5fZI8ebp7WFln8Z8SAQhRIU3KBKCBC8XpXkoOqwvCxXNavQ1AP7tVVWXTANVLWS9AZgA9osUS2TuiqFMBqqG6C04KUvx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b9fuimZi4HZvyP3QajTVcNJcSIuJFr8gcyAovNYeEW5tpnYAeZtY7rPNzN1/C36/dTOaviIbv0P3QPalXEsBEn807Hzm3RAYjM6jWnxQACB4RYHja4mLdktdmr5JhvPB535QmY5g5zHSG8Hngg9UFH/rTvfOe4z+UHsD2/sQnmEz1jvzQsHUrGBYbKptczwg6g3MWkbhU1sxHVc4di3jZxXjsfU/Ug2eKx7eoWexlUONjab2niQNxaQfLvsk7sS47lWYSsQTt63QGVduv7Xtx5qgN2DZJPESV9QfJFgATsJt5SVosE4MHhY3zMk/OUFeUZI4w6pYfp+/RaOlSbTEAADtZKH5m+dm/I/dDVszfPH1+6B+/FIepjFnzmL+31+6pfjndvr90DypjJVL8ZCRnGO7KQxB6D6oGzcWhazryhPxR6D6/deVcSeg+v3QEF6rKC/EHsvffnsg//2Q==";
base64Data = binaryData.replace(/^data:image\/jpeg;base64,/, "");
binaryData = new Buffer(base64Data, 'base64').toString('binary');
console.log(binaryData)
fs.writeFile("out1.jpg", binaryData, "binary", function (err) {//working
if(err)
console.log("ERROR : "+err); // writes out file without error, but it's not a valid image
});
fs.writeFile("out2.jpg", body, "binary", function (err) { //not working
if(err)
console.log("ERROR : "+err); // writes out file without error, but it's not a valid image
});
Replace
fs.writeFile(path1, body, {encoding: 'base64'}, function(err){
});
With
fs.writeFile(path1, 'data:image/jpeg;base64,' + new Buffer(body).toString('base64'), {encoding: 'base64'}, function(err){
});
这篇关于在Node.js中保存从URL保存图像的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!