هناك طرق متعددة لتحويل جداول HTML into جداول بيانات JSON و CSV و Excel باستخدام GrabzIt's Node.js API، مفصلة هنا هي بعض من التقنيات الأكثر فائدة. ولكن قبل أن تبدأ تذكر أنه بعد استدعاء url_to_table, html_to_table or file_to_table طرق save or save_to يجب استدعاء الأسلوب لالتقاط الجدول. إذا كنت تريد أن ترى بسرعة ما إذا كانت هذه الخدمة مناسبة لك ، فيمكنك تجربة عرض حي لالتقاط جداول HTML من URL.
ستحول هذه المكالمة بطريقة معينة جدول HTML الأول في صفحة الويب الخاصة بعنوان URL المحدد ، 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
سيؤدي هذا افتراضيًا إلى تحويل الجدول الأول الذي يحدده intجدول يا. ومع ذلك ، يمكن تحويل الجدول الثاني في صفحة ويب عن طريق تمرير 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; } });
باستخدام Node.js و GrabzIt ، يمكنك تحويل جداول HTML intس JSON ، حدد فقط json
في المعلمة تنسيق. كما هو مبين في المثال أدناه مرة واحدة save_to
تم الانتهاء من الطريقة يتم استدعاء الدالة oncomplete مع 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; } });