Narzędzia do przechwytywania i konwertowania Internetu

Przechwytuj tabele HTML ze stron internetowych za pomocą Java

Java API

Istnieje wiele sposobów konwersji tabel HTML into Używanie arkuszy kalkulacyjnych JSON, CSV i Excel Java API GrabzIt, tutaj wyszczególniono niektóre z najbardziej przydatnych technik. Jednak zanim zaczniesz, pamiętaj, że po wywołaniu URLToTable, HTMLToTable or FileToTable metody Save or SaveTo należy wywołać metodę, aby przechwycić tabelę. Jeśli chcesz szybko sprawdzić, czy ta usługa jest właśnie dla Ciebie, możesz spróbować pokaz na żywo przechwytywania tabel HTML z adresu URL.

Podstawowe opcje

Ten fragment kodu przekształci pierwszą tabelę HTML znalezioną na określonej stronie internetowej into dokument 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

Domyślnie spowoduje to konwersję pierwszej zidentyfikowanej tabeli into stole. Jednak drugą tabelę na stronie internetowej można przekonwertować, przekazując 2 do setTableNumberToInclude metoda TableOptions class.

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");

Można również użyć setTargetElement Metoda zapewniająca konwersję tylko tabel w ramach określonego identyfikatora elementu.

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");

Alternatywnie możesz przechwycić wszystkie tabele na stronie internetowej, przekazując true do setIncludeAllTables Metoda ta będzie jednak działać tylko z formatami XLSX i JSON. Ta opcja umieści każdą tabelę w nowym arkuszu w wygenerowanym skoroszycie arkusza kalkulacyjnego.

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");

Konwertuj tabele HTML na JSON

GrabzIt może także konwertować tabele HTML znalezione w sieci na JSON, po prostu określ format JSON. W poniższym przykładzie dane są odczytywane synchronicznie i zwracane jako GrabzItFile obiekt za pomocą SaveTo metoda, jednak ogólnie zaleca się to zrobić asynchronicznie.

Po zakończeniu konwersji toString wywoływana jest metoda, aby uzyskać JSON jako string, można to następnie przeanalizować za pomocą biblioteki takiej jak 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();
}

Niestandardowy identyfikator

Możesz przekazać niestandardowy identyfikator do stół metody pokazane poniżej, ta wartość jest następnie zwracana do programu obsługi Java GrabzIt. Na przykład ten niestandardowy identyfikator może być identyfikatorem bazy danych, umożliwiając skojarzenie zrzutu ekranu z określonym rekordem bazy danych.

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");