i want to download bing's todays wallpaper by node.js,
first of all,i can download large pictures from some other sites,
but i can't download the pic from bing,even though i can see the pic in my browser
the example picture src is : "http://www.bing.com/az/hprichbg?p=rb%2fYosemiteSnow_EN-US7191433727_1920x1200.jpg"
here's my core code
var download_file_httpget = function(file_url) {
var options = {
host: url.parse(file_url).host,
port: 80,
path: url.parse(file_url).pathname,
headers: {}
};
var file_name = '11.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');
});
});
};
for example,the orginal pic's size is 200k,but it only download 20k,and i doubt that it is because of the 'referer' in the header,when i add the header,the problem fixed in some oher sites,but it still dosen't work for bing
Switching to
path: url.parse(file_url).path,
will include the query string and get you the entire file.
The file that gets downloaded is an HTML text file, not jpeg.
console.log(url.parse(file_url));
Shows this:
{
protocol: 'http:',
slashes: true,
host: 'www.bing.com',
hostname: 'www.bing.com',
href: 'http://www.bing.com/az/hprichbg?p=rb%2fYosemiteSnow_EN-US7191433727_1920x1200.jpg',
search: '?p=rb%2fYosemiteSnow_EN-US7191433727_1920x1200.jpg',
query: 'p=rb%2fYosemiteSnow_EN-US7191433727_1920x1200.jpg',
pathname: '/az/hprichbg',
path: '/az/hprichbg?p=rb%2fYosemiteSnow_EN-US7191433727_1920x1200.jpg'
}
Since you are using pathname
, the url is:
http://www.bing.com/az/hprichbg
so Bing doesn't know which image to return.