Convertir tablas HTML into JSON, CSV y hojas de cálculo Excel usando API de Ruby de GrabzIt hacia el este solo siga los ejemplos que se muestran aquí. Sin embargo, antes de comenzar, recuerde que después de llamar al url_to_table, html_to_table or file_to_table métodos de save or save_to 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.
El siguiente ejemplo convierte la primera tabla HTML en una página web específica into un documento CSV.
grabzItClient.url_to_table("https://www.tesla.com") # Then call the save or save_to method
grabzItClient.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
grabzItClient.file_to_table("tables.html") # Then call the save or save_to method
Si no desea convertir automáticamente la primera tabla en una página web, puede especificar el tableNumberToInclude
método. Por ejemplo, especificar un 2 convertiría la segunda tabla encontrada en una página web.
grabzItClient = GrabzIt::Client.new("Sign in to view your Application Key", "Sign in to view your Application Secret") options = GrabzIt::TableOptions.new() options.tableNumberToInclude = 2 grabzItClient.url_to_table("https://www.tesla.com", options) # Then call the save or save_to method grabzItClient.save_to("result.csv"
grabzItClient = GrabzIt::Client.new("Sign in to view your Application Key", "Sign in to view your Application Secret") options = GrabzIt::TableOptions.new() options.tableNumberToInclude = 2 grabzItClient.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 grabzItClient.save_to("result.csv")
grabzItClient = GrabzIt::Client.new("Sign in to view your Application Key", "Sign in to view your Application Secret") options = GrabzIt::TableOptions.new() options.tableNumberToInclude = 2 grabzItClient.file_to_table("tables.html", options) # Then call the save or save_to method grabzItClient.save_to("result.csv")
También puede especificar la targetElement
Método que garantizará que solo se conviertan las tablas dentro del ID del elemento especificado.
grabzItClient = GrabzIt::Client.new("Sign in to view your Application Key", "Sign in to view your Application Secret") options = GrabzIt::TableOptions.new() options.targetElement = "stocks_table" grabzItClient.url_to_table("https://www.tesla.com", options) # Then call the save or save_to method grabzItClient.save_to("result.csv")
grabzItClient = GrabzIt::Client.new("Sign in to view your Application Key", "Sign in to view your Application Secret") options = GrabzIt::TableOptions.new() options.targetElement = "stocks_table" grabzItClient.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 grabzItClient.save_to("result.csv")
grabzItClient = GrabzIt::Client.new("Sign in to view your Application Key", "Sign in to view your Application Secret") options = GrabzIt::TableOptions.new() options.targetElement = "stocks_table" grabzItClient.file_to_table("tables.html", options) # Then call the save or save_to method grabzItClient.save_to("result.csv")
Si usa el formato XLSX, puede capturar todas las tablas en una página web pasando fiel a includeAllTables
método. Esto colocará cada tabla en una nueva hoja dentro del libro de la hoja de cálculo.
grabzItClient = GrabzIt::Client.new("Sign in to view your Application Key", "Sign in to view your Application Secret") options = GrabzIt::TableOptions.new() options.format = "xlsx" options.includeAllTables = true grabzItClient.url_to_table("https://www.tesla.com", options) # Then call the save or save_to method grabzItClient.save_to("result.xlsx")
grabzItClient = GrabzIt::Client.new("Sign in to view your Application Key", "Sign in to view your Application Secret") options = GrabzIt::TableOptions.new() options.format = "xlsx" options.includeAllTables = true grabzItClient.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 grabzItClient.save_to("result.xlsx")
grabzItClient = GrabzIt::Client.new("Sign in to view your Application Key", "Sign in to view your Application Secret") options = GrabzIt::TableOptions.new() options.format = "xlsx" options.includeAllTables = true grabzItClient.file_to_table("tables.html", options) # Then call the save or save_to method grabzItClient.save_to("result.xlsx")
Con GrabzIt, Ruby puede convertir fácilmente tablas HTML into JSON para hacer esto especificar json
en el formato de parámetro. En el siguiente ejemplo se leen los datos sincrónicamente mediante el uso de la save_to
método, para obtener el JSON como string. Esto puede ser analizado por una biblioteca como json gem.
grabzItClient = GrabzIt::Client.new("Sign in to view your Application Key", "Sign in to view your Application Secret") options = GrabzIt::TableOptions.new() options.format = "json" options.tableNumberToInclude = 1 grabzItClient.url_to_table("https://www.tesla.com", options) json = grabzItClient.save_to()
grabzItClient = GrabzIt::Client.new("Sign in to view your Application Key", "Sign in to view your Application Secret") options = GrabzIt::TableOptions.new() options.format = "json" options.tableNumberToInclude = 1 grabzItClient.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) json = grabzItClient.save_to()
grabzItClient = GrabzIt::Client.new("Sign in to view your Application Key", "Sign in to view your Application Secret") options = GrabzIt::TableOptions.new() options.format = "json" options.tableNumberToInclude = 1 grabzItClient.file_to_table("tables.html", options) json = grabzItClient.save_to()
Puede pasar un identificador personalizado a mesa métodos como se muestra a continuación, este valor se devuelve a su controlador GrabzIt Ruby. 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::Client.new("Sign in to view your Application Key", "Sign in to view your Application Secret") options = GrabzIt::TableOptions.new() options.customId = "123456" grabzItClient.url_to_table("https://www.tesla.com", options) # Then call the save method grabzItClient.save("http://www.example.com/handler/index")
grabzItClient = GrabzIt::Client.new("Sign in to view your Application Key", "Sign in to view your Application Secret") options = GrabzIt::TableOptions.new() options.customId = "123456" grabzItClient.html_to_table("<html><body><h1>Hello World!</h1></body></html>", options) # Then call the save method grabzItClient.save("http://www.example.com/handler/index")
grabzItClient = GrabzIt::Client.new("Sign in to view your Application Key", "Sign in to view your Application Secret") options = GrabzIt::TableOptions.new() options.customId = "123456" grabzItClient.file_to_table("example.html", options) # Then call the save method grabzItClient.save("http://www.example.com/handler/index")