Capturing icons with node.js

I have a small program that needs to capture icons from sites using node.js. This works in most cases, but with apple.com I get errors that I cannot understand or fix:

    var sys= require('sys');
    var fs= require('fs');
    var http = require('http');

    var rurl = http.createClient(80, 'apple.com');
    var requestUrl = 'http://www.apple.com/favicon.ico';
    var request = rurl.request('GET', requestUrl, {"Content-Type": "image/x-icon", "host" : "apple.com" });
    request.end();

    request.addListener('response', function (response)
    {
            response.setEncoding('binary');
            var body = '';
            response.addListener('data', function (chunk) {
                    body += chunk;
            });
            response.addListener("end", function() {
            });
    });

When I make this request, the response will be as follows:

<head><body> This object may be found <a HREF="http://www.apple.com/favicon.ico">here</a> </body>

As a result, I changed the above code in almost every possible way with the hostname options in the client creation step and the url request using "www.apple.com", but usually I just get errors from node as follows:

node.js:63
    throw e;
    ^
Error: Parse Error
    at Client.ondata (http:901:22)
    at IOWatcher.callback (net:494:29)
    at node.js:769:9

Also, I am not interested in using google service to capture icons.

+3
source share
2 answers

This code works for me

var sys = require("sys")
  , fs = require("fs")
  , http = require("http");

var client = http.createClient(80, "www.apple.com") // Change of hostname here and below
  , req = client.request( "GET"
                        , "http://www.apple.com/favicon.ico"
                        , {"Host": "www.apple.com"});

req.end();

req.addListener("response", function (res) {
  var body = "";
  res.setEncoding('binary');
  res.addListener("data", function (c) {
    body += c;
  });
  res.addListener("end", function () {
    // Do stuff with body
  });
});
+1
source

www.apple.com ( www), Content-Type ? . Accept: image/x-icon

URL:

$ curl -I http://www.apple.com/favicon.ico
HTTP/1.1 200 OK
Last-Modified: Thu, 12 Mar 2009 17:09:30 GMT
ETag: "1036-464ef0c1c8680"
Server: Apache/2.2.11 (Unix)
X-Cache-TTL: 568
X-Cached-Time: Thu, 21 Jan 2010 14:55:37 GMT
Accept-Ranges: bytes
Content-Length: 4150
Content-Type: image/x-icon
Cache-Control: max-age=463
Expires: Sun, 12 Sep 2010 14:22:09 GMT
Date: Sun, 12 Sep 2010 14:14:26 GMT
Connection: keep-alive

... .

, non-www:

$ curl -I http://www.apple.com/favicon.ico -H Host: apple.com
HTTP/1.0 400 Bad Request
Server: AkamaiGHost
Mime-Version: 1.0
Content-Type: text/html
Content-Length: 208
Expires: Sun, 12 Sep 2010 14:25:03 GMT
Date: Sun, 12 Sep 2010 14:25:03 GMT
Connection: close

HTTP/1.1 302 Object Moved
Location: http://www.apple.com/
Content-Type: text/html
Cache-Control: private
Connection: close

, , , , .

+4

Source: https://habr.com/ru/post/1764348/


All Articles