[英]How do I join separate json objects output from a for loop into an array?
我正在使用CasperJS抓取網站,其中一項任務涉及通過for循環計數器設置的網址爬行。 網址看起來像這樣
www.example.com/page/no=
其中no是for循環計數器設置的0-10中的任何數字。 然后刮刀遍歷所有頁面,將數據刮入JSON對象並重復直到no = 10。
我想要獲取的數據存儲在每個頁面中的離散組中 - 我想要使用的是通過連接每個頁面的所有刮取輸出的單個JSON對象。
想象一下Page1有Expense 1,我得到的對象是{expense1},而Page 2有Expense 2,而我得到的對象是{expense2}。 我想要的是在抓取結束時的一個JSON,如下所示:
scrapedData = {
"expense1": expense1,
"expense2": expense2,
}
我遇到的麻煩是將所有JSON對象加入到一個數組中。
我初始化了一個空數組,然后每個對象都被推送到數組。 我已經嘗試了一個檢查,如果迭代器我在for循環中等於10,那么JSON對象打印出來但似乎沒有用。 我向上看,似乎對象傳播是一個選項,但我不知道如何使用它這種情況。
任何指針都會有所幫助。 我應該使用像map這樣的任何數組函數嗎?
casper.then(function(){
var url = "https:example.net/secure/SaFinShow?url=";
//We create a for loop to go open the urls
for (i=0; i<11; i++){
this.thenOpen(url+ i, function(response){
expense_amount = this.fetchText("td[headers='amount']");
Date = this.fetchText("td[headers='Date']");
Location = this.fetchText("td[headers='zipcode']");
id = this.fetchText("td[headers='id']");
singleExpense = {
"Expense_Amount": expense_amount,
"Date": Date,
"Location": Location,
"id": id
};
if (i ===10){
expenseArray.push(JSON.stringify(singleExpense, null, 2))
this.echo(expenseArray);
}
});
};
});
以你的榜樣和擴展它為例,你應該能夠做到這樣的事情:
// Initialize empty object to hold all of the expenses
var scrapedData = {};
casper.then(function(){
var url = "https:example.net/secure/SaFinShow?url=";
//We create a for loop to go open the urls
for (i=0; i<11; i++){
this.thenOpen(url+ i, function(response){
expense_amount = this.fetchText("td[headers='amount']");
Date = this.fetchText("td[headers='Date']");
Location = this.fetchText("td[headers='zipcode']");
id = this.fetchText("td[headers='id']");
singleExpense = {
"Expense_Amount": expense_amount,
"Date": Date,
"Location": Location,
"id": id
};
// As we loop over each of the expenses add them to the object containing all of them
scrapedData['expense'+i] = singleExpense;
});
};
});
運行后, scrapedData
變量應該是以下形式:
scrapedData = {
"expense1": expense1,
"expense2": expense2
}
上面代碼的一個問題是,當你循環開支時,for循環中的變量應該是本地的。 變量名也不應該是Date
和Location
因為它們是JavaScript中的內置名稱。
// Initialize empty object to hold all of the expenses
var scrapedData = {};
casper.then(function(){
var url = "https:example.net/secure/SaFinShow?url=";
//We create a for loop to go open the urls
for (i=0; i<11; i++){
this.thenOpen(url+ i, function(response){
// Create our local variables to store data for this particular
// expense data
var expense_amount = this.fetchText("td[headers='amount']");
// Don't use `Date` it is a JS built-in name
var date = this.fetchText("td[headers='Date']");
// Don't use `Location` it is a JS built-in name
var location = this.fetchText("td[headers='zipcode']");
var id = this.fetchText("td[headers='id']");
singleExpense = {
"Expense_Amount": expense_amount,
"Date": date,
"Location": location,
"id": id
};
// As we loop over each of the expenses add them to the object containing all of them
scrapedData['expense'+i] = singleExpense;
});
};
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.