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

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

ממשק ה- API של Node.js

ישנן מספר דרכים להמיר טבלאות HTML into גיליונות אלקטרוניים של JSON, CSV ו- Excel באמצעות ממשק ה- API של GrabzIt Node.jsלהלן כמה מהטכניקות השימושיות ביותר. עם זאת לפני שתתחיל לזכור את זה לאחר התקשר אל url_to_table, html_to_table or file_to_table שיטות save or save_to יש לקרוא לשיטה ללכידת הטבלה. אם אתה רוצה לראות במהירות אם שירות זה מתאים לך, אתה יכול לנסות הדגמה חיה של לכידת טבלאות HTML מכתובת אתר.

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

שיחת שיטה ספציפית זו תמיר את טבלת ה- HTML הראשונה בדף האינטרנט של כתובת האתר שצוינה, intoa מסמך CSV. קטע קוד זה ימיר את טבלת ה- HTML הראשונה שנמצאה בדף אינטרנט או קלט HTML מוגדר intoa מסמך CSV.

client.url_to_table("https://www.tesla.com");
//Then call the save or save_to method
client.html_to_table("<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 save_to method
client.file_to_table("tables.html");
//Then call the save or save_to method

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

var grabzit = require('grabzit');

var client = new grabzit("Sign in to view your Application Key", "Sign in to view your Application Secret");

var options = {"tableNumberToInclude":2};

client.url_to_table("https://www.tesla.com", options);
//Then call the save or save_to method
client.save_to("result.csv", function (error, id){
    //this callback is called once the capture is downloaded
    if (error != null){
        throw error;
    }
});
var grabzit = require('grabzit');

var client = new grabzit("Sign in to view your Application Key", "Sign in to view your Application Secret");

var options = {"tableNumberToInclude":2};

client.html_to_table("<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 save_to method
client.save_to("result.csv", function (error, id){
    //this callback is called once the capture is downloaded
    if (error != null){
        throw error;
    }
});
var grabzit = require('grabzit');

var client = new grabzit("Sign in to view your Application Key", "Sign in to view your Application Secret");

var options = {"tableNumberToInclude":2};

client.file_to_table("tables.html", options);
//Then call the save or save_to method
client.save_to("result.csv", function (error, id){
    //this callback is called once the capture is downloaded
    if (error != null){
        throw error;
    }
});

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

var grabzit = require('grabzit');

var client = new grabzit("Sign in to view your Application Key", "Sign in to view your Application Secret");

var options = {"targetElement":"stocks_table"};

client.url_to_table("https://www.tesla.com", options);
//Then call the save or save_to method
client.save_to("result.csv", function (error, id){
    //this callback is called once the capture is downloaded
    if (error != null){
        throw error;
    }
});
var grabzit = require('grabzit');

var client = new grabzit("Sign in to view your Application Key", "Sign in to view your Application Secret");

var options = {"targetElement":"stocks_table"};

client.html_to_table("<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 save_to method
client.save_to("result.csv", function (error, id){
    //this callback is called once the capture is downloaded
    if (error != null){
        throw error;
    }
});
var grabzit = require('grabzit');

var client = new grabzit("Sign in to view your Application Key", "Sign in to view your Application Secret");

var options = {"targetElement":"stocks_table"};

client.file_to_table("tables.html", options);
//Then call the save or save_to method
client.save_to("result.csv", function (error, id){
    //this callback is called once the capture is downloaded
    if (error != null){
        throw error;
    }
});

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

var grabzit = require('grabzit');

var client = new grabzit("Sign in to view your Application Key", "Sign in to view your Application Secret");

var options = {"format","xlsx","includeHeaderNames":true,"includeAllTables":true};

client.url_to_table("https://www.tesla.com", options);
//Then call the save or save_to method
client.save_to("result.xlsx", function (error, id){
    //this callback is called once the capture is downloaded
    if (error != null){
        throw error;
    }
});
var grabzit = require('grabzit');

var client = new grabzit("Sign in to view your Application Key", "Sign in to view your Application Secret");

var options = {"format","xlsx","includeHeaderNames":true,"includeAllTables":true};

client.html_to_table("<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 save_to method
client.save_to("result.xlsx", function (error, id){
    //this callback is called once the capture is downloaded
    if (error != null){
        throw error;
    }
});
var grabzit = require('grabzit');

var client = new grabzit("Sign in to view your Application Key", "Sign in to view your Application Secret");

var options = {"format","xlsx","includeHeaderNames":true,"includeAllTables":true};

client.file_to_table("tables.html", options);
//Then call the save or save_to method
client.save_to("result.xlsx", function (error, id){
    //this callback is called once the capture is downloaded
    if (error != null){
        throw error;
    }
});

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

באמצעות Node.js ו- GrabzIt תוכלו להמיר טבלאות HTML into JSON, פשוט ציין json בפרמטר הפורמט. כפי שמוצג בדוגמה להלן פעם אחת save_to השיטה הסתיימה הפונקציה הלא-מושלמת נקראת עם JSON במשתנה התוצאה זה מנותח על ידי ה- Node.js המובנה. JSON.parse פונקציה ליצירת אובייקט המייצג את טבלת ה- HTML.

var grabzit = require('grabzit');

var client = new grabzit("Sign in to view your Application Key", "Sign in to view your Application Secret");

var options = {"format","json","includeHeaderNames":true,"includeAllTables":true};
client.url_to_table("https://www.tesla.com", options);

client.save_to(null, function(error, result){
    if (result != null)
    {
        var tableObj = JSON.parse(result);
    }
});

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

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

var grabzit = require('grabzit');

var client = new grabzit("Sign in to view your Application Key", "Sign in to view your Application Secret");

var options = {"customId":123456};

client.url_to_table("https://www.tesla.com", options);
//Then call the save method
client.save("http://www.example.com/handler", function (error, id){
    if (error != null){
        throw error;
    }
});
var grabzit = require('grabzit');

var client = new grabzit("Sign in to view your Application Key", "Sign in to view your Application Secret");

var options = {"customId":123456};

client.html_to_table("<html><body><h1>Hello World!</h1></body></html>", options);
//Then call the save method
client.save("http://www.example.com/handler", function (error, id){
    if (error != null){
        throw error;
    }
});
var grabzit = require('grabzit');

var client = new grabzit("Sign in to view your Application Key", "Sign in to view your Application Secret");

var options = {"customId":123456};

client.file_to_table("example.html", options);
//Then call the save method
client.save("http://www.example.com/handler", function (error, id){
    if (error != null){
        throw error;
    }
});