滚动到页面底部有两种方法:
- 使用 scrollIntoView (滚动到页面底部可能会生成更多内容的部分) 和选择器(例如,
document.querySelectorAll('.class_name').length
检查是否生成了更多内容)
- 使用 scrollBy (逐步向下滚动页面)和 setTimeout 或者 setInterval (逐步检查是否滚动到页面底部)
这里是一种使用纯JavaScript中的
scrollIntoView
和选择器实现的方式(假设
.class_name
是用于滚动到更多内容的选择器),我们可以在浏览器中运行:
方法1: 使用 scrollIntoView 和选择器const delay = 3000;
const wait = (ms) => new Promise(res => setTimeout(res, ms));
const count = async () => document.querySelectorAll('.class_name').length;
const scrollDown = async () => {
document.querySelector('.class_name:last-child')
.scrollIntoView({ behavior: 'smooth', block: 'end', inline: 'end' });
}
let preCount = 0;
let postCount = 0;
do {
preCount = await count();
await scrollDown();
await wait(delay);
postCount = await count();
} while (postCount > preCount);
await wait(delay);
在这种方法中,我们通过比较滚动前(
preCount
)和滚动后(
postCount
)
.class_name
选择器的数量来检查我们是否到达页面底部。
if (postCount > precount) {
} else {
}
以下是两种使用JavaScript中的setTimeout
或setInterval
与scrollBy
实现滚动的示例,我们可以在浏览器控制台中运行:
方法2a:使用setTimeout与scrollBy
const distance = 100;
const delay = 100;
while (document.scrollingElement.scrollTop + window.innerHeight < document.scrollingElement.scrollHeight) {
document.scrollingElement.scrollBy(0, distance);
await new Promise(resolve => { setTimeout(resolve, delay); });
}
方法2b:使用setInterval和scrollBy
const distance = 100;
const delay = 100;
const timer = setInterval(() => {
document.scrollingElement.scrollBy(0, distance);
if (document.scrollingElement.scrollTop + window.innerHeight >= document.scrollingElement.scrollHeight) {
clearInterval(timer);
}
}, delay);
在这种方法中,我们将document.scrollingElement.scrollTop + window.innerHeight
与document.scrollingElement.scrollHeight
进行比较,以检查是否已滚动到页面底部:
if (document.scrollingElement.scrollTop + window.innerHeight < document.scrollingElement.scrollHeight) {
} else {
}
如果上述JavaScript代码之一将页面滚动到底部,那么我们就知道它正在工作,并且我们可以使用Puppeteer自动化此过程。以下是示例Puppeteer Node.js脚本,它将向下滚动到页面底部并等待几秒钟后关闭浏览器。
Puppeteer方法1:使用选择器(.class_name
)的scrollIntoViewconst puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch({
headless: false,
defaultViewport: null,
args: ['--window-size=800,600']
});
const page = await browser.newPage();
await page.goto('https://example.com');
const delay = 3000;
let preCount = 0;
let postCount = 0;
do {
preCount = await getCount(page);
await scrollDown(page);
await page.waitFor(delay);
postCount = await getCount(page);
} while (postCount > preCount);
await page.waitFor(delay);
await browser.close();
})();
async function getCount(page) {
return await page.$$eval('.class_name', a => a.length);
}
async function scrollDown(page) {
await page.$eval('.class_name:last-child', e => {
e.scrollIntoView({ behavior: 'smooth', block: 'end', inline: 'end' });
});
}
Puppeteer方法2a:使用setTimeout与scrollBy
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch({
headless: false,
defaultViewport: null,
args: ['--window-size=800,600']
});
const page = await browser.newPage();
await page.goto('https://example.com');
await scrollToBottom(page);
await page.waitFor(3000);
await browser.close();
})();
async function scrollToBottom(page) {
const distance = 100;
const delay = 100;
while (await page.evaluate(() => document.scrollingElement.scrollTop + window.innerHeight < document.scrollingElement.scrollHeight)) {
await page.evaluate((y) => { document.scrollingElement.scrollBy(0, y); }, distance);
await page.waitFor(delay);
}
}
Puppeteer方法2b:使用setInterval与scrollBy
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch({
headless: false,
defaultViewport: null,
args: ['--window-size=800,600']
});
const page = await browser.newPage();
await page.goto('https://example.com');
await page.evaluate(scrollToBottom);
await page.waitFor(3000);
await browser.close();
})();
async function scrollToBottom() {
await new Promise(resolve => {
const distance = 100;
const delay = 100;
const timer = setInterval(() => {
document.scrollingElement.scrollBy(0, distance);
if (document.scrollingElement.scrollTop + window.innerHeight >= document.scrollingElement.scrollHeight) {
clearInterval(timer);
resolve();
}
}, delay);
});
}
await page.evaluate('window.scrollTo(0, document.body.scrollHeight)')
。 - Ondrej Kvasnovsky