هناك طرق متعددة لتحويل جداول HTML into جداول بيانات JSON و CSV و Excel باستخدام GrabzIt في ASP.NET API، مفصلة هنا هي بعض من التقنيات الأكثر فائدة. ولكن قبل أن تبدأ تذكر أنه بعد استدعاء URLToTable, HTMLToTable or FileToTable طرق Save or SaveTo يجب استدعاء الأسلوب لالتقاط الجدول. إذا كنت تريد أن ترى بسرعة ما إذا كانت هذه الخدمة مناسبة لك ، فيمكنك تجربة عرض حي لالتقاط جداول HTML من URL.
يحول المثال التالي من التعليمات البرمجية جدول 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
سيؤدي هذا افتراضيًا إلى تحويل الجدول الأول الذي يحدده intجدول يا. ومع ذلك ، يمكن تحويل الجدول الثاني في صفحة ويب عن طريق تمرير 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");
يمكن لـ GrabzIt أيضًا تحويل جداول HTML إلى JSON ، ما عليك سوى تحديد تنسيق JSON كما هو موضح أدناه. نحن هنا نقرأ البيانات بشكل متزامن intيا ال GrabzItFile
الاعتراض باستخدام SaveTo
الطريقة ، ومع ذلك فمن المستحسن عموما أن تفعل هذا غير متزامن بدلا من ذلك.
بمجرد أن لدينا النتيجة نحصل على string تمثيل ملف JSON عن طريق استدعاء ToString
الطريقة ، يمكن بعد ذلك إلغاء تسلسلها 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");