我實際上試圖從CasperJs中的網站獲取一些數據。數據存儲在一張桌子裏。 我試圖在廢品後得到正確的JSON文件。 json: - 公司名稱, - 郵件, - 網站 - 活動描述。CasperJS循環遍歷表和JSON輸出的抓取數據
到目前爲止,我已經能夠打開頁面並獲取數據,但不是精確的(郵件和網站是相同的)。所以我發現如何精確選擇每個我想要的元素。 但在這種情況下,我沒有得到所有表信息,只有第一行...
我會知道是否有人可以幫助我,告訴我在哪裏看或如何使循環在我的情況?假設我不是專業的開發者,我正在訓練自己。
這裏我的代碼:
var casper = require('casper').create();
var url = 'http://www.rent2016.fr/pages/exposants';
var fs = require('fs');
var length;
casper.start(url);
casper.then(function() {
this.waitForSelector('table#myTable');
});
casper.then(function(){
var info = this.evaluate(function(){
var table_rows = document.querySelectorAll("tr"); //or better selector
return Array.prototype.map.call(table_rows, function(tr){
return {
nom : document.querySelector(".td-width h3").textContent,
description: document.querySelector(".td-width p").textContent,
mail : document.querySelector("td span a").textContent,
site : document.querySelector('td span a[href^="http"]').textContent,
};
});
});
fs.write('test_rent_stringify.json', JSON.stringify(info), 'w');
this.echo(JSON.stringify(info, undefined, 4));
});
casper.run(function() {
});
在這裏,我們沒有循環:JSON重複第一行的信息的。爲了讓每一個行的信息必須由
nom : tr.children[1].textContent,
更換
nom : document.querySelector(".td-width h3").textContent,
但在這種情況下,你不能精確定位的H3,鏈接......你得到所有的信息。所以,其實我可以:
遍歷行和獲得信息,但他們無法使用
只有第一排的信息,但事先有良好的表現
謝謝!
也許這篇文章可以幫助你 - > http://stackoverflow.com/questions/41273739/how-do-scrape-table-from-the -provided-website-using-casperjs/41302965#41302965你只需要用鏈接的解決方案寫一個Obejct /數組/文件的結果,如你所願。 – dasmelch
謝謝,但我已經檢查過這個帖子。 這是發生同樣的問題:我不能精確地在行內選擇我想要的數據。但更重要的是,在這裏我沒有得到所有DATAS,只有公司的標題和描述。這是在相同的。 – ArnaudHMZ