Use los siguientes ejemplos de cómo convertir tablas HTML into JSON, CSV y hojas de cálculo Excel usando GrabzIt Perl API. Sin embargo, antes de comenzar, recuerde que después de llamar al URLToTable, HTMLToTable or FileToTable métodos de Save or SaveTo El método debe ser recuperar la captura de 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 ejemplo de código que se encuentra a continuación convierte automáticamente la primera tabla HTML descubierta 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 tableNumberToInclude
método.
$grabzIt = GrabzItClient->new("Sign in to view your Application Key", "Sign in to view your Application Secret"); $options = GrabzItTableOptions->new(); $options->tableNumberToInclude(2); $grabzIt->URLToTable("https://www.tesla.com", $options); # Then call the Save or SaveTo method $grabzIt->SaveTo("result.csv");
$grabzIt = GrabzItClient->new("Sign in to view your Application Key", "Sign in to view your Application Secret"); $options = GrabzItTableOptions->new(); $options->tableNumberToInclude(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");
$grabzIt = GrabzItClient->new("Sign in to view your Application Key", "Sign in to view your Application Secret"); $options = GrabzItTableOptions->new(); $options->tableNumberToInclude(2); $grabzIt->FileToTable("tables.html", $options); # Then call the Save or SaveTo method $grabzIt->SaveTo("result.csv");
También puede especificar la targetElement
Método que garantizará que solo se conviertan las tablas dentro del ID del elemento especificado.
$grabzIt = GrabzItClient->new("Sign in to view your Application Key", "Sign in to view your Application Secret"); $options = GrabzItTableOptions->new(); $options->targetElement("stocks_table"); $grabzIt->URLToTable("https://www.tesla.com", $options); # Then call the Save or SaveTo method $grabzIt->SaveTo("result.csv");
$grabzIt = GrabzItClient->new("Sign in to view your Application Key", "Sign in to view your Application Secret"); $options = GrabzItTableOptions->new(); $options->targetElement("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");
$grabzIt = GrabzItClient->new("Sign in to view your Application Key", "Sign in to view your Application Secret"); $options = GrabzItTableOptions->new(); $options->targetElement("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 includeAllTables
Sin embargo, esto solo funcionará con el formato XLSX. Esta opción colocará cada tabla en una nueva hoja dentro del libro de hoja de cálculo generado.
$grabzIt = GrabzItClient->new("Sign in to view your Application Key", "Sign in to view your Application Secret"); $options = GrabzItTableOptions->new(); $options->format('xlsx'); $options->includeAllTables(1); $grabzIt->URLToTable("https://www.tesla.com", $options); # Then call the Save or SaveTo method $grabzIt->SaveTo("result.xlsx");
$grabzIt = GrabzItClient->new("Sign in to view your Application Key", "Sign in to view your Application Secret"); $options = GrabzItTableOptions->new(); $options->format('xlsx'); $options->includeAllTables(1); $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");
$grabzIt = GrabzItClient->new("Sign in to view your Application Key", "Sign in to view your Application Secret"); $options = GrabzItTableOptions->new(); $options->format('xlsx'); $options->includeAllTables(1); $grabzIt->FileToTable("tables.html", $options); # Then call the Save or SaveTo method $grabzIt->SaveTo("result.xlsx");
Con Perl y Grabz, puede convertir las tablas HTML encontradas into JSON. Para comenzar, especifique el json
en el formato de parámetro. En el ejemplo estamos haciendo un llamada sincrónica mediante el uso de la SaveTo
método, que devuelve el JSON string, esto podría ser analizado por una biblioteca como JSON :: Parse.
$grabzIt = GrabzItClient->new("Sign in to view your Application Key", "Sign in to view your Application Secret"); $options = GrabzItTableOptions->new(); $options->format("json"); $options->tableNumberToInclude(1); $grabzIt->URLToTable("https://www.tesla.com", $options); $json = $grabzIt->SaveTo();
$grabzIt = GrabzItClient->new("Sign in to view your Application Key", "Sign in to view your Application Secret"); $options = GrabzItTableOptions->new(); $options->format("json"); $options->tableNumberToInclude(1); $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); $json = $grabzIt->SaveTo();
$grabzIt = GrabzItClient->new("Sign in to view your Application Key", "Sign in to view your Application Secret"); $options = GrabzItTableOptions->new(); $options->format("json"); $options->tableNumberToInclude(1); $grabzIt->FileToTable("tables.html", $options); $json = $grabzIt->SaveTo();
Puede pasar un identificador personalizado a mesa métodos como se muestra a continuación, este valor se devuelve a su controlador GrabzIt Perl. 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.
$grabzIt = GrabzItClient->new("Sign in to view your Application Key", "Sign in to view your Application Secret"); $options = GrabzItTableOptions->new(); $options->customId("123456"); $grabzIt->URLToTable("https://www.tesla.com", $options); # Then call the Save method $grabzIt->Save("http://www.example.com/handler.pl");
$grabzIt = GrabzItClient->new("Sign in to view your Application Key", "Sign in to view your Application Secret"); $options = GrabzItTableOptions->new(); $options->customId("123456"); $grabzIt->HTMLToTable("<html><body><h1>Hello World!</h1></body></html>", $options); # Then call the Save method $grabzIt->Save("http://www.example.com/handler.pl");
$grabzIt = GrabzItClient->new("Sign in to view your Application Key", "Sign in to view your Application Secret"); $options = GrabzItTableOptions->new(); $options->customId("123456"); $grabzIt->FileToTable("example.html", $options); # Then call the Save method $grabzIt->Save("http://www.example.com/handler.pl");