כלים ללכידה ולהמרה של האינטרנט

ללכוד טבלאות HTML מאתרי אינטרנט עם ASP.NET

ממשק API של ASP.NET

ישנן מספר דרכים להמיר טבלאות HTML into גיליונות אלקטרוניים של JSON, CSV ו- Excel באמצעות ממשק ה- API של ASP.NET של GrabzItלהלן כמה מהטכניקות השימושיות ביותר. עם זאת לפני שתתחיל לזכור את זה לאחר התקשר אל URLToTable, HTMLToTable or FileToTable שיטות Save or SaveTo יש לקרוא לשיטה ללכידת הטבלה. אם אתה רוצה לראות במהירות אם שירות זה מתאים לך, אתה יכול לנסות הדגמה חיה של לכידת טבלאות HTML מכתובת אתר.

אפשרויות בסיסיות

דוגמת הקוד הבאה ממירה את טבלת ה- HTML הראשונה בדף אינטרנט שצוין intoa מסמך 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

כברירת מחדל זה ימיר את הטבלה הראשונה שהיא מזהה intoa שולחן. עם זאת ניתן להמיר את הטבלה השנייה בדף אינטרנט על ידי העברת 2 לטבלה TableNumberToInclude נכס.

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

אתה יכול גם לציין את TargetElement נכס שיבטיח רק טבלאות בתוך מזהה האלמנט שצוין יומרו.

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

לחלופין אתה יכול ללכוד את כל הטבלאות בדף אינטרנט על ידי העברת נאמן ל- IncludeAllTables עם זאת, הדבר יעבוד רק בפורמט XLSX או JSON. אם תבחר בפורמט XSLX כל טבלה תכניס גיליון חדש בתוך חוברת העבודה של הגיליון האלקטרוני שנוצר.

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

המרת טבלאות HTML ל- JSON

GrabzIt יכול גם להמיר טבלאות HTML ל- JSON, פשוט ציין את פורמט JSON כמוצג להלן. כאן אנו קוראים את הנתונים באופן סינכרוני into ה GrabzItFile באמצעות אובייקט SaveTo עם זאת, בדרך כלל מומלץ לעשות זאת באופן אסינכרוני במקום.

ברגע שיש לנו את התוצאה אנו מקבלים את string ייצוג של קובץ JSON על ידי התקשרות ל- ToString בשיטה, ניתן לאחר מכן deserialized intאובייקט דינמי באמצעות ספריית 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();
}

מזהה מותאם אישית

אתה יכול להעביר מזהה מותאם אישית אל ה- שולחן שיטות כמוצג להלן, ערך זה מוחזר לאחר מכן למטפל GrabzIt ASP.NET שלך. לדוגמה, מזהה מותאם אישית זה יכול להיות מזהה בסיס נתונים, המאפשר לשייך צילום מסך לרשומת מסד נתונים מסוימת.

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