nodejs下载图片时遇到防外链的图片无法下载
发布于 11 年前 作者 rppig42 6914 次浏览 最后一次编辑是 8 年前

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');
	});
});

};

3 回复

加上referer试试

下载百度的图片加了referer以后是就可以了,但下载bing首页wallpaper就不行! 服务器能够识破http头的伪造请求吗

@rppig42 http头都是由客户端构建的,应该问题不大,你可以用firebug看一下浏览器请求图片的header头,完全照搬过来,看看是否与User-Agent这些有关

回到顶部