我想刮一页,作为一个练习学习幻影,但我有一个问题,目前。图像加载被推迟,所以我试图弄清楚如何才能让幻影js向下滚动,等待图像加载。到页面底部的SCrolling无法工作,所以我想每3秒滚动100 it,直到它到达页面底部为止。我怎样才能做到这一点呢?
const phantom = require('phantom');
(async function() {
const instance = await phantom.create();
const page = await instance.createPage();
await page.on('onResourceRequested', function(requestData) {
console.info('Requesting', requestData.url);
});
await page.open(<URL>);
const js = await page.includeJs('http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js');
const data = await page.evaluate(function() {
// Do something
});
page.render('test.pdf');
await page.close();
await instance.exit();
})();发布于 2018-01-09 09:31:13
PhantomJS确实支持“滚动”,有一个页面属性scrollPosition可能会像这样使用:
await page.property('scrollPosition', { top: 300, left: 0 });您可以动态地更改scrollPosition,在时间内增加它,这将触发负责图像加载的回调。
下面是原始一个例子脚本中的PhantomJS,它展示了在Twitter的时间线上运行的技术。
发布于 2018-01-04 10:34:53
您也可以使用基于节点-网络快照的phantom.js来呈现pdf。它有很多配置。你需要的是renderDelay延迟截屏,shotOffset滚动到你想要的地方。
发布于 2019-10-07 16:25:06
const phantom = require('phantom');
// Scrolls the page till new content is available
async function scrollPage(page) {
const currentContentLength = (await page.property('content')).length;
await page.evaluate(function () {
window.document.body.scrollTop = document.body.scrollHeight;
});
await wait(Math.max(5000, 10000 * Math.random()));
const nextContentLength = (await page.property('content')).length;
if (currentContentLength != nextContentLength) {
console.log("Scrolling page:", await page.property('url'), "for more content");
await scrollPage(page);
}
}
// Scrolls the page and gets the page content using PhantomJS
async function getPageData(pageUrl, shouldScrollPage) {
const instance = await phantom.create();
const page = await instance.createPage();
await page.open(pageUrl);
if (shouldScrollPage) {
await scrollPage(page);
}
const pageContent = await page.property('content');
await page.close();
await instance.exit();
return pageContent;
};https://stackoverflow.com/questions/48089441
复制相似问题