Existem várias maneiras de converter tabelas HTML into JSON, CSV e planilhas do Excel usando API Node.js do GrabzIt, detalhadas aqui estão algumas das técnicas mais úteis. No entanto, antes de começar, lembre-se de que depois de ligar para o url_to_table, html_to_table or file_to_table métodos os save or save_to O método deve ser chamado para capturar a tabela. Se você quiser ver rapidamente se este serviço é adequado para você, tente uma demonstração ao vivo da captura de tabelas HTML de um URL.
Essa chamada de método específica converterá a primeira tabela HTML na página da Web do URL especificado, into um documento CSV. Esse snippet de código converterá a primeira tabela HTML encontrada em uma página da Web ou entrada HTML especificada into um documento CSV.
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
Por padrão, isso converterá a primeira tabela que identifica intuma mesa. No entanto, a segunda tabela em uma página da web pode ser convertida passando um 2 para o tableNumberToInclude
propriedade.
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; } });
Você também pode especificar o targetElement
A propriedade que garantirá que apenas as tabelas dentro do ID do elemento especificado serão convertidas.
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; } });
Como alternativa, você pode capturar todas as tabelas em uma página da Web passando true para o includeAllTables
, no entanto, isso funcionará apenas com os formatos JSON e XLSX. Essa opção colocará cada tabela em uma nova planilha na pasta de trabalho da planilha gerada.
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; } });
Usando Node.js e GrabzIt, você pode converter tabelas HTML into JSON, basta especificar json
no parâmetro format. Como mostrado no exemplo abaixo, uma vez que o save_to
Quando o método é concluído, a função oncomplete é chamada com o JSON na variável result, que é então analisada pelo Node.js embutido. JSON.parse
para criar um objeto que representa a tabela HTML.
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); } });
Você pode passar um identificador personalizado para o mesa Como mostrado abaixo, esse valor é retornado ao seu manipulador GrabzIt Node.js. Por exemplo, esse identificador personalizado pode ser um identificador de banco de dados, permitindo que uma captura de tela seja associada a um registro específico do banco de dados.
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; } });