Nodejs下载图片时遇到防外链的图片无法下载
Nodejs下载图片时遇到防外链的图片无法下载
rt,我用nodejs下载比方说新浪相册这种没做防外链的图片时都没有问题,但是去下载比如百度这种防外链的图片时,都下载不下来,有什么解决方案吗? 我的关键代码如下,
var download_file_httpget = function(file_url) {
var options = {
host: url.parse(file_url).host,
port: 80,
path: url.parse(file_url).pathname
};
//use the date as the file name
var date = new Date();
var file_name = (date.getMonth()+1).toString() + '-' + date.getDate().toString();
file_name += '.jpg';
var file = fs.createWriteStream(download_path + "\\" + file_name);
http.get(options,function(res) {
res.on('data',function(data) {
file.write(data);
}).on('end',function() {
file.end();
console.log('download success');
});
});
};
4 回复
Node.js 下载图片时遇到防外链的图片无法下载
问题描述
我在使用 Node.js 下载图片时遇到了一个问题。当我尝试下载像新浪相册这种没有防外链限制的图片时,一切都很顺利。然而,当我去下载像百度这种做了防外链限制的图片时,下载就失败了。有没有什么解决方案呢?
关键代码
这是我的关键代码:
var download_file_httpget = function(file_url) {
var options = {
host: url.parse(file_url).host,
port: 80,
path: url.parse(file_url).pathname
};
// 使用日期作为文件名
var date = new Date();
var file_name = (date.getMonth()+1).toString() + '-' + date.getDate().toString();
file_name += '.jpg';
var file = fs.createWriteStream(download_path + "\\" + file_name);
http.get(options, function(res) {
res.on('data', function(data) {
file.write(data);
}).on('end', function() {
file.end();
console.log('download success');
});
});
};
解决方案
为了能够下载被防外链保护的图片,我们需要模拟浏览器的行为,即发送带有 Referer
头的请求。这样可以欺骗服务器,使其认为我们是从允许访问的网站进行请求的。
以下是一个改进后的示例代码:
const https = require('https');
const fs = require('fs');
const url = require('url');
const download_path = './downloads'; // 保存图片的路径
const file_url = 'https://example.com/image.jpg'; // 需要下载的图片 URL
const options = {
hostname: url.parse(file_url).hostname,
port: 443,
path: url.parse(file_url).path,
method: 'GET',
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
'Referer': 'https://www.example.com' // 模拟来自允许访问的网站的请求
}
};
// 使用日期作为文件名
const date = new Date();
const file_name = `${date.getMonth()+1}-${date.getDate()}.jpg`;
const file = fs.createWriteStream(`${download_path}/${file_name}`);
https.request(options, (res) => {
res.pipe(file);
res.on('end', () => {
console.log('download success');
});
}).on('error', (err) => {
console.error(`Error: ${err.message}`);
file.close();
});
解释
options
对象:我们设置了hostname
、port
、path
和method
,同时添加了User-Agent
和Referer
头。https.request
:我们使用https.request
方法来发送请求,并通过res.pipe(file)
将响应流直接写入到文件中。- 错误处理:我们在请求过程中添加了错误处理逻辑,以便在发生错误时输出错误信息并关闭文件流。
通过这种方式,我们可以绕过防外链限制,成功下载受保护的图片。
加上referer试试
下载百度的图片加了referer以后是就可以了,但下载bing首页wallpaper就不行! 服务器能够识破http头的伪造请求吗
要解决Node.js下载被防外链保护的图片的问题,可以尝试使用带有请求头(headers)的HTTP请求来模拟浏览器的行为。许多网站通过检查请求头中的User-Agent或Referer字段来防止外部链接访问资源。
以下是修改后的示例代码,它包含了必要的请求头:
const https = require('https');
const fs = require('fs');
var download_file_https = function(file_url, download_path) {
var options = {
hostname: new URL(file_url).hostname,
path: new URL(file_url).pathname,
method: 'GET',
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'
}
};
// 使用日期作为文件名
const date = new Date();
const file_name = `${(date.getMonth() + 1)}-${date.getDate()}.jpg`;
const file = fs.createWriteStream(`${download_path}/${file_name}`);
const req = https.request(options, (res) => {
res.pipe(file);
res.on('end', () => {
console.log('Download successful');
});
});
req.on('error', (e) => {
console.error(`Problem with request: ${e.message}`);
});
req.end();
};
在这个例子中,我们使用了https.request
而不是http.get
,因为很多网站使用HTTPS。同时,添加了User-Agent
头部,模拟了一个常见的浏览器环境,这可能有助于绕过某些防外链策略。