Er zijn meerdere manieren om HTML-tabellen te converteren into JSON, CSV's en Excel-spreadsheets gebruiken GrabzIt's Node.js API, hier worden enkele van de meest nuttige technieken beschreven. Onthoud echter voordat u begint dat nadat u de url_to_table, html_to_table or file_to_table methoden de save or save_to methode moet worden aangeroepen om de tabel vast te leggen. Als u snel wilt zien of deze service geschikt is voor u, kunt u een proberen live demo van het vastleggen van HTML-tabellen van een URL.
Deze specifieke methodeaanroep converteert de eerste HTML-tabel op de webpagina van de opgegeven URL, intoa CSV-document. Dit codefragment converteert de eerste HTML-tabel die op een specifieke webpagina of HTML-invoer wordt gevonden intoa CSV-document.
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
Standaard converteert dit de eerste tabel die wordt geïdentificeerd inteen tafel. De tweede tabel op een webpagina kan echter worden geconverteerd door een 2 door te geven aan de tableNumberToInclude
eigendom.
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; } });
U kunt ook de targetElement
eigenschap die ervoor zorgt dat alleen tabellen binnen het opgegeven element-ID worden geconverteerd.
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; } });
Als alternatief kunt u alle tabellen op een webpagina vastleggen door waar door te geven includeAllTables
eigenschap, dit werkt echter alleen met de indelingen JSON en XLSX. Met deze optie wordt elke tabel in een nieuw blad in de gegenereerde spreadsheetwerkmap geplaatst.
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; } });
Door Node.js en GrabzIt te gebruiken, kunt u HTML-tabellen converteren into JSON, specificeer gewoon json
in de opmaakparameter. Zoals getoond in het onderstaande voorbeeld zodra de save_to
methode is voltooid, wordt de functie oncomplete aangeroepen met de JSON in de resultaatvariabele. Deze wordt vervolgens ontleed door de ingebouwde Node.js JSON.parse
functie om een object te maken dat de HTML-tabel vertegenwoordigt.
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); } });
U kunt een aangepaste identificatie doorgeven aan de tafel methoden zoals hieronder weergegeven, wordt deze waarde vervolgens teruggegeven aan uw GrabzIt Node.js-handler. Deze aangepaste identifier kan bijvoorbeeld een database-identifier zijn, waardoor een screenshot aan een bepaald database-record kan worden gekoppeld.
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; } });