ישנן מספר דרכים להמיר טבלאות HTML into גיליונות אלקטרוניים של JSON, CSV ו- Excel באמצעות ממשק ה- API של 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 לטבלה setTableNumberToInclude
השיטה של TableOptions מעמד.
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");
אתה יכול גם להשתמש setTargetElement
שיטה להבטיח שרק טבלאות בתוך מזהה האלמנט שצוין יומרו.
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");
לחלופין אתה יכול ללכוד את כל הטבלאות בדף אינטרנט על ידי העברת נאמן ל- setIncludeAllTables
עם זאת, פעולה זו תעבוד רק בפורמטים XLSX ו- JSON. אפשרות זו תכניס כל טבלה לגיליון חדש בתוך חוברת העבודה של הגיליון האלקטרוני שנוצר.
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");
GrabzIt יכול גם להמיר טבלאות HTML שנמצאו באינטרנט ל- JSON, פשוט ציין במקום זאת את פורמט JSON. בדוגמה למטה הנתונים נקראים באופן סינכרוני והם מוחזרים כ- GrabzItFile
באמצעות אובייקט SaveTo
עם זאת, בדרך כלל מומלץ לעשות זאת באופן אסינכרוני.
בסיום ההמרה, toString
שיטה נקראת להשיג את JSON בתור string, זה יכול להיות מנותח על ידי ספריה כמו גוגל גיסון.
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(); }
אתה יכול להעביר מזהה מותאם אישית אל ה- שולחן שיטות כמוצג להלן, ערך זה יוחזר למטפל GrabzIt Java שלך. לדוגמה, מזהה מותאם אישית זה יכול להיות מזהה בסיס נתונים, המאפשר לשייך צילום מסך לרשומת מסד נתונים מסוימת.
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");