如何從單個數組中的多個頁面獲取鏈接

比蝸牛還快

我有一個工作代碼,它成功地從多個頁面中獲取所有產品鏈接,這些頁面至少有 20% 的折扣。唯一的問題是它分別返回每個頁面的數組中的鏈接。但是,我希望它返回單個數組中所有頁面的鏈接,然後將它們傳輸到另一個函數。我嘗試創建一個字符串 var all_links = [] 並將每個頁面中的所有鏈接推送到其中,然後像 return all_links 一樣返回它們,正如我從一個更簡單的示例中了解到的那樣。但是,我在這種情況下沒有成功,因為我沒有編碼經驗。三週前我開始學習基礎知識。如果您能幫助我完成整個代碼,我將不勝感激,因為我沒有必要的先驗知識。

const puppeteer = require('puppeteer')
const minDiscount = 20;

async function getLinks() {
    const browser = await puppeteer.launch({
        headless: false,
        defaultViewport: null,
    });
    const page = await browser.newPage();

    const url = 'https://www.mytoys.de/spielzeug-spiele/holz/';

    await page.goto(url);

    // getting all the products, this will return an array of ElementHandle
    while(await page.$(".pager__link--next")){
        await page.waitForSelector(".pager__link--next")
        await page.waitForTimeout(1000);
        await page.click('.pager__link--next')
        await page.waitForTimeout(1500);
        const products = await page.$$('.prod-grid.js-prod-grid .prod-grid__item.js-prod-grid_item');
        const proms = await Promise.allSettled(
            products.map(async (prod) => {
                // searching for a discount on each product
                const disc = await prod.$$eval(
                    '.prod-grid.js-prod-grid .prod-flag.prod-flag-sale',
                    (discount) =>
                        discount.map((discItem) =>
                            discItem.innerText.replace(/[^0-9.]/g, '').replace(/\D+/g,'0')
                        )
                );
                // if it has a discount
                if (disc.length > 0) {
                    // we parse the discount to Integer type to compare it to minDiscount
                    const discountInt = parseInt(disc[0], 10);
                    if (discountInt >= minDiscount) {
                        // we get the link of the product
                        const link = await prod.$$eval('.prod-grid.js-prod-grid .prod-tile__link.js-prodlink', (allAs) => allAs.map((a) => a.href));
                        if (link.length > 0) {
                            // push an object containing the discount and the link of the product
                            return link[0];
                        }
                    }
                }
                return null;
            })
        );
        const bulkArray = proms.map((item) => {
            if (item.status === 'fulfilled') return item.value;
        });
        const endArray = bulkArray.filter(item => item !== null);
        console.log(endArray);
    }
}
    
getLinks();

我目前獲得的結果示例

[
  'https://www.mytoys.de/erzi-kinderwurst-sortiment-spiellebensmittel-6749036.html',
  'https://www.mytoys.de/chr-tanner-spiellebensmittel-wurststaender-1031946.html',
  'https://www.mytoys.de/hape-xylophon-und-hammerspiel-2503719.html',
  'https://www.mytoys.de/erzi-kinderparty-spiellebensmittel-6749035.html',
]
[
  'https://www.mytoys.de/brio-holzeisenbahnset-landleben-5501952.html',
  'https://www.mytoys.de/brio-brio-33277-bahn-ir-reisezug-set-4592516.html',
  'https://www.mytoys.de/brio-parkhaus-strassen-schienen-3175226.html',
  'https://www.mytoys.de/mytoys-steckwuerfel-12-tlg-11389814.html',
  'https://www.mytoys.de/brio-schienen-und-weichensortiment-1758325.html',
]
[
  'https://www.mytoys.de/hape-grosser-baukran-4141517.html',
  'https://www.mytoys.de/noris-mein-buntes-tuermchenspiel-3421170.html',
  'https://www.mytoys.de/goki-ziehtier-schaf-suse-2488933.html',
  'https://www.mytoys.de/eichhorn-colorsoundzug-mit-licht-1521635.html',
]

您想要獲得的結果示例

[
  'https://www.mytoys.de/erzi-kinderwurst-sortiment-spiellebensmittel-6749036.html',
  'https://www.mytoys.de/chr-tanner-spiellebensmittel-wurststaender-1031946.html',
  'https://www.mytoys.de/hape-xylophon-und-hammerspiel-2503719.html',
  'https://www.mytoys.de/erzi-kinderparty-spiellebensmittel-6749035.html',
  'https://www.mytoys.de/brio-holzeisenbahnset-landleben-5501952.html',
  'https://www.mytoys.de/brio-brio-33277-bahn-ir-reisezug-set-4592516.html',
  'https://www.mytoys.de/brio-parkhaus-strassen-schienen-3175226.html',
  'https://www.mytoys.de/mytoys-steckwuerfel-12-tlg-11389814.html',
  'https://www.mytoys.de/brio-schienen-und-weichensortiment-1758325.html',
  'https://www.mytoys.de/hape-grosser-baukran-4141517.html',
  'https://www.mytoys.de/noris-mein-buntes-tuermchenspiel-3421170.html',
  'https://www.mytoys.de/goki-ziehtier-schaf-suse-2488933.html',
  'https://www.mytoys.de/eichhorn-colorsoundzug-mit-licht-1521635.html',
]
謝利
  1. 在循環之前為鏈接收集聲明新變量:
const allLinks = []; // <--
while(await page.$(".pager__link--next")){ ... }
  1. 將所有鏈接推入其中:
...
const endArray = bulkArray.filter(item => item !== null);
console.log(endArray);
allLinks.push(endArray); // <--
  1. 循環執行後返回/記錄結果:
async function getLinks() {
  ...
  return allLinks.flat(); // <--
}

console.log(await getLinks()) // result array

參考: Array.prototype.flat()

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在 Reactjs 的下一個功能組件頁面中從 <Link/> 組件中獲取傳遞數據的值

如何從laravel中的數組中獲取單個值?

如何從 MongoDB 中的數組中獲取多個元素?

如何從單個命令中獲取多個值?

如何從 React 類組件中的兩個動態表單獲取輸入數據?

在 PowerShell 中從多個數組中獲取最多的項目數

如何從列表中獲取n個單詞的連續單詞組合

如何連接4個表以從兩個表中獲取數據

jQuery 選擇:如何從多個選擇中獲取所選選項文本的數組

SQL 如何從多個數據庫中獲取數據?

我如何從具有多個對象、相同數組但只有特定鍵和值的數組中獲取?

嘗試使用 For 循環從多個頁面中捕獲表

如何以 xamarin 形式在單個頁面中播放多個音頻文件

如何使用硒從網頁中獲取所有鏈接?

從一組數組中獲取 10 個 nsmallest 數組

如何從這個 url 中獲取數據?

如何使用beautifulsoup從多個頁面抓取數據

如何從多個數組中找到單個字符串?

如何獲取多維數組中具有特定鍵的最後一個數組

Slick Slider - 從另一個頁面創建鏈接

如何在pdf中將多個圖形保存為單獨的頁面?

如何使用java從android studio中的每個二維數組數據中獲取第一個參數

從元組中獲取前 N 個元素的更簡單方法?

從多個單選按鈕中獲取文本

可以從列表頁面上的 api 中的輔助表中獲取變量,但不能在單個項目頁面上獲取

如何從 tmux 會話中的 VI 複製多個頁面

從具有多個鏈接的數組多次調用 Axios.all 請求

如何消除某些值(零)並從某個數據組中獲取最大值?

如何從具有給定 id 的 2 列的另一個表中獲取或連接數據