我是 JavaScript 新手,所以我現在沒有很好的編程技能,所以我一直在開發一個 Web Scraper,它回傳一個名稱、帖子、生物等陣列,如下所示:
let infoOfPost = await newTab(browser, page);
所以 infoOfPost 是一個陣列,它的值正在變化,因為它在回圈中被呼叫,我可以在控制臺中看到它每次都有 bio、posts、followers 等的新值。但是當我把這個值推送到一個物件時,物件僅在回圈第一次運行時存盤初始值,并且在每次下一次迭代中它只是保持顯示相同的值并且不會覆寫之前的值 Im 在 objec 中存盤陣列為:
accountInfoObject.displayName =infoOfPost[0];
accountInfoObject.posts = infoOfPost[1];
accountInfoObject.followers=infoOfPost[2];
accountInfoObject.following =infoOfPost[3];
accountInfoObject.fullName = infoOfPost[4];
accountInfoObject.about =infoOfPost[5];
accountsInformation.push(accountInfoObject);
await objectsCsv(accountsInformation);
我現在看到的是這樣的:
[
{
accountUrl: 'https://www.example.com/xyz.hij/',
displayName: 'saharpalmer',
posts: '368',
followers: '2,640',
following: '510',
fullName: 'Sahar Intuitive Life Mentor',
about: '30-year Experience: I help you shift your mindset??Get back on track quickly ??Fulfil your purpose & live your best life??'
}
]
我想看到的是,我的所有其他條目后面都帶有逗號,并使其成為物件陣列而不是單個物件陣列。目前我只看到這個一次,這個單個物件的陣列不斷重復。此外,我將此物件推送到一個陣列并將其寫入 Csv 檔案,該檔案也包含此物件,一次又一次重復,如下所示:
about accountUrl displayName posts followers following fullName
30-year Experience: I help you shift your mindset??Get back on track quickly??Fulfil your purpose & live your best life??' https://www.example.com/being.darsh/ saharpalmer 368 2640 510 Sahar
30-year Experience: I help you shift your mindset??Get back on track quickly??Fulfil your purpose & live your best life??' https://www.example.com/being.darsh/ saharpalmer 368 2640 510 Sahar
物件和陣列宣告如下:
let accountsInformation = [];
let accountInfoObject = new Object();
完整代碼是:我們獲取陣列的檔案是:
let accountsInformation = [];
let accountInfoObject = new Object();
async function scrapingPosts(browser, page) {
readCsvFile(urlsToVisit);
for (let x = 0; x < urlsToVisit.length; x ) {
secondaryUrl = urlsToVisit[x];
await page.waitFor(10000);
await page
.goto(`${secondaryUrl}`, {
waitUntil: "domcontentloaded",
})
.catch((e) => {});
await page.waitForSelector("article >div.EZdmt:nth-child(2)",
5000);
for (let i = 1; i < 5; i ) {
await page.waitFor(5000);
// this loops goes through all 3 posts of each container;
for (let j = 1; j <= 3; j ) {
// opening the modal means clicking on post i and j will
increment and we will keep moving to next post 1 by 1
await page.click(
`div.EZdmt > div > div > div:nth-child(${i}) > div:nth-child(${j})`);
let url = await urlOfIds(page, urlsAddress);
await page.waitFor(5000);
let infoOfPost = await newTab(browser, page);
accountInfoObject.accountUrl = url;
accountInfoObject.displayName = infoOfPost[0];
accountInfoObject.posts = infoOfPost[1];
accountInfoObject.followers = infoOfPost[2];
accountInfoObject.following = infoOfPost[3];
accountInfoObject.fullName = infoOfPost[4];
accountInfoObject.about = infoOfPost[5];
await page.waitFor(10000);
accountsInformation.push(accountInfoObject);
console.log(accountsInformation);
await objectsCsv(accountsInformation);
// Modal Closes here process repeats till the loop condition is unsatisfied
await page.click(
"body > div._2dDPU.QPGbb.CkGkG > div.qF0y9._4EzTm.BI4qX.qJPeX.fm1AK.TxciK.yiMZG >button.wpO6b");
await page.waitFor(20000);
}
}
}
await browser.close();
}
infoOfPosts 來自的檔案是:
let evalSelector;
const selectorData = [];
async function newTab(browser, page) {
await page.keyboard.down("Control");
await page.click("span.Jv7Aj.mArmR.MqpiF");
await page.keyboard.up("Control");
await page.waitForTimeout(1000);
const newPage = (await browser.pages())[1];
await newPage.waitForNavigation("#react-root");
await newPage.waitFor(20000);
evalSelector = await selectorEvaluation(newPage, titleSelector);
selectorData.push(evalSelector);
evalSelector = await selectorEvaluation(newPage, noPostSelector);
selectorData.push(evalSelector);
evalSelector = await selectorEvaluation(newPage,
noOfFollowersSelector);
selectorData.push(evalSelector);
evalSelector = await selectorEvaluation(newPage,
noOfFollowingSelector);
selectorData.push(evalSelector);
evalSelector = await selectorEvaluation(newPage,
displayNameSelector);
selectorData.push(evalSelector);
evalSelector = await selectorEvaluation(newPage, aboutSelector);
selectorData.push(evalSelector);
console.log(selectorData);
await newPage.waitFor(5000);
await newPage.close();
return selectorData;
}
module.exports = newTab;
任何幫助將非常感激。提前致謝。點贊!!
uj5u.com熱心網友回復:
你有兩個問題。
- 您
accountInfoObject每次都在重復使用相同的內容accountsInformation.push(accountInfoObject);。
let accountsInformation = [];
async function scrapingPosts(browser, page) {
readCsvFile(urlsToVisit);
for (let x = 0; x < urlsToVisit.length; x ) {
secondaryUrl = urlsToVisit[x];
await page.waitFor(10000);
await page
.goto(`${secondaryUrl}`, {
waitUntil: "domcontentloaded",
})
.catch((e) => {});
await page.waitForSelector("article >div.EZdmt:nth-child(2)",
5000);
for (let i = 1; i < 5; i ) {
await page.waitFor(5000);
// this loops goes through all 3 posts of each container;
for (let j = 1; j <= 3; j ) {
// opening the modal means clicking on post i and j will
increment and we will keep moving to next post 1 by 1
await page.click(
`div.EZdmt > div > div > div:nth-child(${i}) > div:nth-child(${j})`);
let url = await urlOfIds(page, urlsAddress);
await page.waitFor(5000);
let infoOfPost = await newTab(browser, page);
let accountInfoObject = {
accountUrl: url,
displayName: infoOfPost[0],
posts: infoOfPost[1],
followers: infoOfPost[2],
following: infoOfPost[3],
fullName: infoOfPost[4],
about: infoOfPost[5]
};
await page.waitFor(10000);
accountsInformation.push(accountInfoObject);
console.log(accountsInformation);
await objectsCsv(accountsInformation);
// Modal Closes here process repeats till the loop condition is unsatisfied
await page.click(
"body > div._2dDPU.QPGbb.CkGkG > div.qF0y9._4EzTm.BI4qX.qJPeX.fm1AK.TxciK.yiMZG >button.wpO6b");
await page.waitFor(20000);
}
}
}
await browser.close();
}
- 你不清除
selectorData你推新資料之前newTab()。您應該將其設為區域變數。因此,當scrapingPosts()使用元素 0 到 5 時,它們來自獲取的第一篇文章。
async function newTab(browser, page) {
await page.keyboard.down("Control");
await page.click("span.Jv7Aj.mArmR.MqpiF");
await page.keyboard.up("Control");
await page.waitForTimeout(1000);
const newPage = (await browser.pages())[1];
await newPage.waitForNavigation("#react-root");
await newPage.waitFor(20000);
const selectorData = [];
for (let selector of [titleSelector, noPostSelector, noOfFollowersSelector, noOfFollowingSelector, displayNameSelector, aboutSelector]) {
let evalSelector = await selectorEvaluation(newPage, selector);
selectorData.push(evalSelector);
}
console.log(selectorData);
await newPage.waitFor(5000);
await newPage.close();
return selectorData;
}
module.exports = newTab;
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/390123.html
標籤:javascript 节点.js 数组 目的 傀儡师
上一篇:如何將物件陣列轉換為物件物件
下一篇:在while回圈中遞增物件
