Hay varias formas de convertir tablas HTML into JSON, CSV y hojas de cálculo Excel usando API Java de GrabzItAquí se detallan algunas de las técnicas más útiles. Sin embargo, antes de comenzar, recuerde que después de llamar al URLToTable, HTMLToTable or FileToTable métodos de Save or SaveTo Se debe llamar al método para capturar la tabla. Si desea ver rápidamente si este servicio es adecuado para usted, puede probar un demostración en vivo de capturar tablas HTML desde una URL.
Este fragmento de código convertirá la primera tabla HTML encontrada en una página web específica into un documento CSV.
grabzIt.URLToTable("https://www.tesla.com"); //Then call the Save or SaveTo method
grabzIt.HTMLToTable("<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 SaveTo method
grabzIt.FileToTable("tables.html"); //Then call the Save or SaveTo method
Por defecto, esto convertirá la primera tabla que identifica intuna mesa Sin embargo, la segunda tabla en una página web podría convertirse pasando un 2 a setTableNumberToInclude
método de la TableOptions clase.
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret"); TableOptions options = new TableOptions(); options.setTableNumberToInclude(2); grabzIt.URLToTable("https://www.tesla.com", options); //Then call the Save or SaveTo method grabzIt.SaveTo("result.csv");
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret"); TableOptions options = new TableOptions(); options.setTableNumberToInclude(2); grabzIt.HTMLToTable("<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 SaveTo method grabzIt.SaveTo("result.csv");
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret"); TableOptions options = new TableOptions(); options.setTableNumberToInclude(2); grabzIt.FileToTable("tables.html", options); //Then call the Save or SaveTo method grabzIt.SaveTo("result.csv");
También puedes utilizar la setTargetElement
Método para garantizar que solo se convertirán las tablas dentro del ID del elemento especificado.
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret"); TableOptions options = new TableOptions(); options.setTargetElement("stocks_table"); grabzIt.URLToTable("https://www.tesla.com", options); //Then call the Save or SaveTo method grabzIt.SaveTo("result.csv");
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret"); TableOptions options = new TableOptions(); options.setTargetElement("stocks_table"); grabzIt.HTMLToTable("<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 SaveTo method grabzIt.SaveTo("result.csv");
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret"); TableOptions options = new TableOptions(); options.setTargetElement("stocks_table"); grabzIt.FileToTable("tables.html", options); //Then call the Save or SaveTo method grabzIt.SaveTo("result.csv");
Alternativamente, puede capturar todas las tablas en una página web pasando true a setIncludeAllTables
Sin embargo, esto solo funcionará con los formatos XLSX y JSON. Esta opción colocará cada tabla en una nueva hoja dentro del libro de hoja de cálculo generado.
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret"); TableOptions options = new TableOptions(); options.setFormat(TableFormat.XLSX); options.setIncludeAllTables(true); grabzIt.URLToTable("https://www.tesla.com", options); //Then call the Save or SaveTo method grabzIt.SaveTo("result.xlsx");
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret"); TableOptions options = new TableOptions(); options.setFormat(TableFormat.XLSX); options.setIncludeAllTables(true); grabzIt.HTMLToTable("<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 SaveTo method grabzIt.SaveTo("result.xlsx");
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret"); TableOptions options = new TableOptions(); options.setFormat(TableFormat.XLSX); options.setIncludeAllTables(true); grabzIt.FileToTable("tables.html", options); //Then call the Save or SaveTo method grabzIt.SaveTo("result.xlsx");
Grabz También puede convertir tablas HTML que se encuentran en la web a JSON, solo especifique el formato JSON. En el siguiente ejemplo, los datos se leen sincrónicamente y se devuelven como GrabzItFile
objeto mediante el uso de SaveTo
método, sin embargo, generalmente se recomienda que haga esto de forma asíncrona.
Cuando se completa la conversión, el toString
Se llama al método para obtener el JSON como string, esto puede ser analizado por una biblioteca como google gson.
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret"); TableOptions options = new TableOptions(); options.setFormat(TableFormat.JSON); options.setTableNumberToInclude(1); grabzIt.URLToTable("https://www.tesla.com", options); GrabzItFile file = grabzIt.SaveTo(); if (file != null) { String json = file.toString(); }
Puede pasar un identificador personalizado a mesa métodos como se muestra a continuación, este valor se devuelve a su controlador GrabzIt Java. Por ejemplo, este identificador personalizado podría ser un identificador de base de datos, lo que permite asociar una captura de pantalla con un registro de base de datos particular.
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret"); TableOptions options = new TableOptions(); options.setCustomId("123456"); grabzIt.URLToTable("https://www.tesla.com", options); //Then call the Save method grabzIt.Save("http://www.example.com/handler");
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret"); TableOptions options = new TableOptions(); options.setCustomId("123456"); grabzIt.HTMLToTable("<html><body><h1>Hello World!</h1></body></html>", options); //Then call the Save method grabzIt.Save("http://www.example.com/handler");
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret"); TableOptions options = new TableOptions(); options.setCustomId("123456"); grabzIt.FileToTable("example.html", options); //Then call the Save method grabzIt.Save("http://www.example.com/handler");