Tools zum Erfassen und Konvertieren des Webs

Erfassen Sie HTML-Tabellen von Websites mit Node.js

Node.js API

Es gibt mehrere Möglichkeiten, HTML-Tabellen zu konvertieren into JSON-, CSV- und Excel-Tabellen mit GrabzIt's Node.js APIIm Folgenden werden einige der nützlichsten Techniken beschrieben. Bevor Sie jedoch anfangen, denken Sie daran, dass nach dem Aufruf der url_to_table, html_to_table or file_to_table Methoden der save or save_to Es muss eine Methode aufgerufen werden, um die Tabelle zu erfassen. Wenn Sie schnell herausfinden möchten, ob dieser Service für Sie geeignet ist, können Sie a Live-Demo zum Erfassen von HTML-Tabellen von einer URL.

Grundlegende Optionen

Dieser spezielle Methodenaufruf konvertiert die erste HTML-Tabelle auf der Webseite der angegebenen URL. into ein CSV-Dokument. Dieses Code-Snippet konvertiert die erste in einer bestimmten Webseite oder HTML-Eingabe gefundene HTML-Tabelle into ein CSV-Dokument.

client.url_to_table("https://www.tesla.com");
//Then call the save or save_to method
client.html_to_table("<html><body><table><tr><th>Name</th><th>Age</th></tr>
    <tr><td>Tom</td><td>23</td></tr><tr><td>Nicola</td><td>26</td></tr>
    </table></body></html>");
//Then call the save or save_to method
client.file_to_table("tables.html");
//Then call the save or save_to method

Standardmäßig wird die erste identifizierte Tabelle konvertiert into ein Tisch. Die zweite Tabelle auf einer Webseite kann jedoch konvertiert werden, indem ein 2 an das übergeben wird tableNumberToInclude Eigentum.

var grabzit = require('grabzit');

var client = new grabzit("Sign in to view your Application Key", "Sign in to view your Application Secret");

var options = {"tableNumberToInclude":2};

client.url_to_table("https://www.tesla.com", options);
//Then call the save or save_to method
client.save_to("result.csv", function (error, id){
    //this callback is called once the capture is downloaded
    if (error != null){
        throw error;
    }
});
var grabzit = require('grabzit');

var client = new grabzit("Sign in to view your Application Key", "Sign in to view your Application Secret");

var options = {"tableNumberToInclude":2};

client.html_to_table("<html><body><table><tr><th>Name</th><th>Age</th></tr>
    <tr><td>Tom</td><td>23</td></tr><tr><td>Nicola</td><td>26</td></tr>
    </table></body></html>", options);
//Then call the save or save_to method
client.save_to("result.csv", function (error, id){
    //this callback is called once the capture is downloaded
    if (error != null){
        throw error;
    }
});
var grabzit = require('grabzit');

var client = new grabzit("Sign in to view your Application Key", "Sign in to view your Application Secret");

var options = {"tableNumberToInclude":2};

client.file_to_table("tables.html", options);
//Then call the save or save_to method
client.save_to("result.csv", function (error, id){
    //this callback is called once the capture is downloaded
    if (error != null){
        throw error;
    }
});

Sie können auch angeben targetElement Eigenschaft, die sicherstellt, dass nur Tabellen innerhalb der angegebenen Element-ID konvertiert werden.

var grabzit = require('grabzit');

var client = new grabzit("Sign in to view your Application Key", "Sign in to view your Application Secret");

var options = {"targetElement":"stocks_table"};

client.url_to_table("https://www.tesla.com", options);
//Then call the save or save_to method
client.save_to("result.csv", function (error, id){
    //this callback is called once the capture is downloaded
    if (error != null){
        throw error;
    }
});
var grabzit = require('grabzit');

var client = new grabzit("Sign in to view your Application Key", "Sign in to view your Application Secret");

var options = {"targetElement":"stocks_table"};

client.html_to_table("<html><body><table id='stocks_table'><tr><th>Name</th><th>Age</th></tr>
    <tr><td>Tom</td><td>23</td></tr><tr><td>Nicola</td><td>26</td></tr>
    </table></body></html>", options);
//Then call the save or save_to method
client.save_to("result.csv", function (error, id){
    //this callback is called once the capture is downloaded
    if (error != null){
        throw error;
    }
});
var grabzit = require('grabzit');

var client = new grabzit("Sign in to view your Application Key", "Sign in to view your Application Secret");

var options = {"targetElement":"stocks_table"};

client.file_to_table("tables.html", options);
//Then call the save or save_to method
client.save_to("result.csv", function (error, id){
    //this callback is called once the capture is downloaded
    if (error != null){
        throw error;
    }
});

Alternativ können Sie alle Tabellen auf einer Webseite erfassen, indem Sie true an übergeben includeAllTables Dies funktioniert jedoch nur mit den Formaten JSON und XLSX. Mit dieser Option wird jede Tabelle in ein neues Blatt in der generierten Arbeitsmappe eingefügt.

var grabzit = require('grabzit');

var client = new grabzit("Sign in to view your Application Key", "Sign in to view your Application Secret");

var options = {"format","xlsx","includeHeaderNames":true,"includeAllTables":true};

client.url_to_table("https://www.tesla.com", options);
//Then call the save or save_to method
client.save_to("result.xlsx", function (error, id){
    //this callback is called once the capture is downloaded
    if (error != null){
        throw error;
    }
});
var grabzit = require('grabzit');

var client = new grabzit("Sign in to view your Application Key", "Sign in to view your Application Secret");

var options = {"format","xlsx","includeHeaderNames":true,"includeAllTables":true};

client.html_to_table("<html><body><table><tr><th>Name</th><th>Age</th></tr>
    <tr><td>Tom</td><td>23</td></tr><tr><td>Nicola</td><td>26</td></tr>
    </table></body></html>", options);
//Then call the save or save_to method
client.save_to("result.xlsx", function (error, id){
    //this callback is called once the capture is downloaded
    if (error != null){
        throw error;
    }
});
var grabzit = require('grabzit');

var client = new grabzit("Sign in to view your Application Key", "Sign in to view your Application Secret");

var options = {"format","xlsx","includeHeaderNames":true,"includeAllTables":true};

client.file_to_table("tables.html", options);
//Then call the save or save_to method
client.save_to("result.xlsx", function (error, id){
    //this callback is called once the capture is downloaded
    if (error != null){
        throw error;
    }
});

Konvertieren Sie HTML-Tabellen in JSON

Mit Node.js und GrabzIt können Sie HTML-Tabellen konvertieren into JSON, geben Sie einfach an json im Formatparameter. Wie im folgenden Beispiel gezeigt, wird einmal die save_to method is finished Die Oncomplete-Funktion wird mit dem JSON in der Ergebnisvariablen aufgerufen. Diese wird dann von der eingebauten Node.js analysiert JSON.parse Funktion zum Erstellen eines Objekts, das die HTML-Tabelle darstellt.

var grabzit = require('grabzit');

var client = new grabzit("Sign in to view your Application Key", "Sign in to view your Application Secret");

var options = {"format","json","includeHeaderNames":true,"includeAllTables":true};
client.url_to_table("https://www.tesla.com", options);

client.save_to(null, function(error, result){
    if (result != null)
    {
        var tableObj = JSON.parse(result);
    }
});

Benutzerdefinierte Kennung

Sie können der einen benutzerdefinierten Bezeichner übergeben Tabelle Bei den folgenden Methoden wird dieser Wert an den Handler von GrabzIt Node.j zurückgegeben. Diese benutzerdefinierte Kennung kann beispielsweise eine Datenbankkennung sein, mit der ein Screenshot einem bestimmten Datenbankeintrag zugeordnet werden kann.

var grabzit = require('grabzit');

var client = new grabzit("Sign in to view your Application Key", "Sign in to view your Application Secret");

var options = {"customId":123456};

client.url_to_table("https://www.tesla.com", options);
//Then call the save method
client.save("http://www.example.com/handler", function (error, id){
    if (error != null){
        throw error;
    }
});
var grabzit = require('grabzit');

var client = new grabzit("Sign in to view your Application Key", "Sign in to view your Application Secret");

var options = {"customId":123456};

client.html_to_table("<html><body><h1>Hello World!</h1></body></html>", options);
//Then call the save method
client.save("http://www.example.com/handler", function (error, id){
    if (error != null){
        throw error;
    }
});
var grabzit = require('grabzit');

var client = new grabzit("Sign in to view your Application Key", "Sign in to view your Application Secret");

var options = {"customId":123456};

client.file_to_table("example.html", options);
//Then call the save method
client.save("http://www.example.com/handler", function (error, id){
    if (error != null){
        throw error;
    }
});