puppeteer的常用操作方法

1、实现递归滑动页面直到底部

await page.evaluate(async () => {
	await new Promise((resolve, reject) => {
		var totalHeight = 0;
		var distance = 100;
		var timer = setInterval(() => {
			var scrollHeight = document.body.scrollHeight;
			window.scrollBy(0, distance);
			totalHeight += distance;
			if (totalHeight >= scrollHeight) {
				clearInterval(timer);
				resolve();
			}
		}, 100);
	});
});

2、遍历获取表格或者列表属性

productList = await page.evaluate(() => {
    var productList = [...document.querySelectorAll("#productList .product-link")]				
	return productList.map(el => {
		return {
			productId: el.getAttribute("data-product-id")
		}
	});
});

你可能感兴趣的:(技术扩展,node.js)