const puppeteer = require(‘puppeteer’);
let scrape = async () => {
const browser = await puppeteer.launch({headless: false}); //false 则打开一个新的窗口
const page = await browser.newPage();
await page.goto('http://www.zszh77.com/'); //进入页面
await page.click('#goods_list > li:nth-child(1) > a > div.img'); //模拟点击进入新的页面
await page.waitFor(1000); //等一秒
//等待页面加载出来,等同于window.onload
// await page.waitForNavigation({
// waitUntil: 'load'
// });
//await page.waitForNavigation({ waitUntil: "networkidle" }); //等页面完全打开
// console.log(page.url()) //page.url() 当前页面链接
const result = await page.evaluate(() => {
let title = document.querySelector(‘body > div > div.detail > div > div.goods > h1’).innerText; //抓取内容
let price = document.querySelector(‘.price’).innerText;
return {
title,
price
}
});
browser.close();
return result;
};
scrape().then((value) => {
console.log(value); // Success!
});