木偶:如何单击按钮转到另一个页面并获取新页面的内容?

时间:2019-09-23 13:17:09

标签: javascript parsing puppeteer cheerio

我有以下代码:

const puppeteer = require('puppeteer');
const cheerio = require('cheerio');

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

// Get the content of the first page
const page1 = await page.content();
let $ = cheerio.load(page1);
let items = $($('div[id*=document]')).toArray();

// Go to another page
const buttonToPage2 = await page.$('li[data-page="2"]');
await buttonToPage2.click();

// Get the content from the second page
const page2 = await page.content();
let $ = cheerio.load(page2);
items = items.concat($($('div[id*=document]')).toArray());

不是吗?看来我没有从第2页上获得正确的内容。 无论如何,您知道一种更好的方法吗?

0 个答案:

没有答案