Tools zum Erfassen und Konvertieren des Webs

Erfassen Sie HTML-Tabellen von Websites mit ASP.NET

ASP.NET API

Es gibt mehrere Möglichkeiten, HTML-Tabellen zu konvertieren into JSON-, CSV- und Excel-Tabellen mit GrabzIt's ASP.NET APIIm Folgenden werden einige der nützlichsten Techniken beschrieben. Bevor Sie jedoch anfangen, denken Sie daran, dass nach dem Aufruf der URLToTable, HTMLToTable or FileToTable Methoden der Save or SaveTo Es muss eine Methode aufgerufen werden, um die Tabelle zu erfassen. Wenn Sie schnell herausfinden möchten, ob dieser Service für Sie geeignet ist, können Sie a Live-Demo zum Erfassen von HTML-Tabellen von einer URL.

Grundlegende Optionen

Im folgenden Codebeispiel wird die erste HTML-Tabelle in einer angegebenen Webseite konvertiert into ein CSV-Dokument.

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

Standardmäßig wird die erste identifizierte Tabelle konvertiert into ein Tisch. Die zweite Tabelle auf einer Webseite kann jedoch konvertiert werden, indem ein 2 an das übergeben wird TableNumberToInclude Eigentum.

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

Sie können auch angeben TargetElement Eigenschaft, die sicherstellt, dass nur Tabellen innerhalb der angegebenen Element-ID konvertiert werden.

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

Alternativ können Sie alle Tabellen auf einer Webseite erfassen, indem Sie true an übergeben IncludeAllTables Dies funktioniert jedoch nur im XLSX- oder JSON-Format. Wenn Sie das XSLX-Format auswählen, wird jede Tabelle in der generierten Tabellenkalkulations-Arbeitsmappe in ein neues Blatt eingefügt.

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

Konvertieren Sie HTML-Tabellen in JSON

GrabzIt kann auch HTML-Tabellen in JSON konvertieren. Geben Sie einfach das JSON-Format wie unten gezeigt an. Hier lesen wir die Daten synchron into die GrabzItFile Objekt mithilfe der SaveTo Es wird jedoch generell empfohlen, dies zu tun asynchron stattdessen.

Sobald wir das Ergebnis haben, erhalten wir die string Darstellung der JSON-Datei durch Aufruf der ToString Methode kann diese dann deserialisiert werden into Ein dynamisches Objekt, das Ihre bevorzugte JSON-Bibliothek verwendet.

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();
}

Benutzerdefinierte Kennung

Sie können der einen benutzerdefinierten Bezeichner übergeben Tabelle Bei den folgenden Methoden wird dieser Wert an Ihren GrabzIt ASP.NET-Handler zurückgegeben. Diese benutzerdefinierte Kennung kann beispielsweise eine Datenbankkennung sein, mit der ein Screenshot einem bestimmten Datenbankeintrag zugeordnet werden kann.

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