簡體   English   中英

使用 Puppeteer 抓取多個網站

[英]Scrape multiple websites using Puppeteer

所以我試圖從多個網站(在這種情況下是 PS Store)中抓取兩個元素。 此外,我試圖以最簡單的方式實現它。 由於我是 JS 新手,請保持溫和 ;) 在我的腳本下方。 我試圖用 for 循環讓它發生但沒有效果(它仍然只從數組中獲得第一個網站)。 非常感謝您提供的任何幫助。

const puppeteer = require("puppeteer");

async function scrapeProduct(url) {
  const urls = [
    "https://store.playstation.com/pl-pl/product/EP9000-CUSA09176_00-DAYSGONECOMPLETE",
    "https://store.playstation.com/pl-pl/product/EP9000-CUSA13323_00-GHOSTSHIP0000000",
  ];
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  for (i = 0; i < urls.length; i++) {
    const url = urls[i];
    const promise = page.waitForNavigation({ waitUntil: "networkidle" });
    await page.goto(`${url}`);
    await promise;
  }

  const [el] = await page.$x(
    "/html/body/div[3]/div/div/div[2]/div/div/div[2]/h2"
  );
  const txt = await el.getProperty("textContent");
  const title = await txt.jsonValue();

  const [el2] = await page.$x(
    "/html/body/div[3]/div/div/div[2]/div/div/div[1]/div[2]/div[1]/div[1]/h3"
  );
  const txt2 = await el2.getProperty("textContent");
  const price = await txt2.jsonValue();

  console.log({ title, price });

  browser.close();
}

scrapeProduct();

一般來說,你的代碼是相當不錯的。 不過,應該糾正一些事情:

const puppeteer = require("puppeteer");

async function scrapeProduct(url) {
    const urls = [
        "https://store.playstation.com/pl-pl/product/EP9000-CUSA09176_00-DAYSGONECOMPLETE",
        "https://store.playstation.com/pl-pl/product/EP9000-CUSA13323_00-GHOSTSHIP0000000",
    ];
    const browser = await puppeteer.launch({
        headless: false
    });
    for (i = 0; i < urls.length; i++) {
        const page = await browser.newPage();
        const url = urls[i];
        const promise = page.waitForNavigation({
            waitUntil: "networkidle2"
        });
        await page.goto(`${url}`);
        await promise;
        const [el] = await page.$x(
            "/html/body/div[3]/div/div/div[2]/div/div/div[2]/h2"
        );
        const txt = await el.getProperty("textContent");
        const title = await txt.jsonValue();

        const [el2] = await page.$x(
            "/html/body/div[3]/div/div/div[2]/div/div/div[1]/div[2]/div[1]/div[1]/h3"
        );
        const txt2 = await el2.getProperty("textContent");
        const price = await txt2.jsonValue();

        console.log({
            title,
            price
        });

    }
    browser.close();
}

scrapeProduct();
  1. 您在循環中打開網頁,這是正確的,然后在循環外查找元素。 為什么? 您應該在循環內進行。
  2. 對於調試,我建議使用{ headless: false } 這使您可以查看瀏覽器中實際發生的情況。
  3. 不確定您使用的是哪個版本的 puppeteer,但是在npm最新版本中沒有諸如networkidle之類的事件。 您應該改用networkidle0networkidle2
  4. 您正在通過 xpath html/body/div...尋找元素。 這可能是主觀的,但我認為標准的 JS/CSS 選擇器更具可讀性: body > div ... 但是,好吧,如果它有效......

在我的情況下,上面的代碼產生以下結果:

{ title: 'Days Gone™', price: '289,00 zl' }
{ title: 'Ghost of Tsushima', price: '289,00 zl' }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM