首页 > 解决方案 > Puppeteer 返回以数字为键、空对象为值的对象

问题描述

所以,我正在研究一个网络爬虫,我需要返回一组链接,为此:

const puppeteer = require('puppeteer');

const URL = 'SOME_URL';

const SELECTOR = 'SOME_SELECTOR'

const app = async () => {

  try {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();
    await page.goto(URL,{waitUntil: 'networkidle0'});
    

    await page.waitForSelector(SELECTOR);

    let content = await page.evaluate(()=> {
      
      let episodes = document.querySelectorAll(SELECTOR);
      return episodes;
    })

    console.log(content)
    await browser.close()
  }catch(err) {
    console.log(err)
  }


}

我把这个作为输出,

  {
  '0': {},
  '1': {},
  '2': {},
  '3': {},
  '4': {},
  '5': {},
  '6': {},
  '7': {},
  '8': {},
  '9': {},
... so on
    }

任何指针为什么会发生,我尝试使用其他选择器甚至其他 URL。

标签: node.jsweb-scrapingpuppeteer

解决方案


Unfortunately, page.evaluate() can only transfer serializable values (roughly, the values JSON can handle). As document.querySelectorAll() returns collection of DOM elements which are not serializable (they contain methods and circular references), each element in the collection is replaced with an empty object. You need to return either serializable value (for example, an array of hrefs) or use something like page.$$(selector) and ElementHandle API.


推荐阅读