Narzędzia do przechwytywania i konwertowania Internetu

Przechwytuj tabele HTML ze stron internetowych za pomocą ASP.NET

Interfejs API ASP.NET

Istnieje wiele sposobów konwersji tabel HTML into Używanie arkuszy kalkulacyjnych JSON, CSV i Excel Interfejs API GrabzIt ASP.NET, 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

Poniższy przykład kodu konwertuje pierwszą tabelę HTML 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 TableNumberToInclude własność.

GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

TableOptions options = new TableOptions();
options.TableNumberToInclude = 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.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");
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

TableOptions options = new TableOptions();
options.TableNumberToInclude = 2;

grabzIt.FileToTable("tables.html", options);
//Then call the Save or SaveTo method
grabzIt.SaveTo("result.csv");

Możesz także określić TargetElement właściwość, która zapewni 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.TargetElement = "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.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");
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

TableOptions options = new TableOptions();
options.TargetElement = "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 IncludeAllTables , jednak będzie to działać tylko z formatem XLSX lub JSON. Jeśli wybierzesz format XSLX, każda tabela zostanie umieszczona 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.Format = TableFormat.xlsx;
options.IncludeAllTables = 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.Format = TableFormat.xlsx;
options.IncludeAllTables = 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.Format = TableFormat.xlsx;
options.IncludeAllTables = 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 na JSON, wystarczy określić format JSON, jak pokazano poniżej. Tutaj odczytujemy dane synchronicznie into GrabzItFile obiekt za pomocą SaveTo metoda, jednak ogólnie zaleca się to zrobić asynchronicznie zamiast.

Po uzyskaniu wyniku otrzymujemy string reprezentacja pliku JSON poprzez wywołanie ToString Metoda ta może zostać następnie przekształcona w postaci szeregowej into obiekt dynamiczny korzystający z Twojej ulubionej biblioteki JSON.

GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

TableOptions options = new TableOptions();
options.Format = TableFormat.json;
options.TableNumberToInclude = 1;

grabzIt.URLToTable("https://www.tesla.com", options);

GrabzItFile file = grabzIt.SaveTo();
if (file != null)
{
    string json = file.ToString();
}
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

TableOptions options = new TableOptions();
options.Format = TableFormat.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);

GrabzItFile file = grabzIt.SaveTo();
if (file != null)
{
    string json = file.ToString();
}
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

TableOptions options = new TableOptions();
options.Format = TableFormat.json;
options.TableNumberToInclude = 1;

grabzIt.FileToTable("tables.html", options);

GrabzItFile file = grabzIt.SaveTo();
if (file != null)
{
    string json = file.ToString();
}

Niestandardowy identyfikator

Możesz przekazać niestandardowy identyfikator do stół metody, jak pokazano poniżej, ta wartość jest następnie zwracana do modułu obsługi GrabzIt ASP.NET. 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.CustomId = "123456";

grabzIt.URLToTable("https://www.tesla.com", options);
//Then call the Save method
grabzIt.Save("http://www.example.com/Home/Handler");
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

TableOptions options = new TableOptions();
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/Home/Handler");
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

TableOptions options = new TableOptions();
options.CustomId = "123456";

grabzIt.FileToTable("example.html", options);
//Then call the Save method
grabzIt.Save("http://www.example.com/Home/Handler");