节点 js 和 Phantom js
Node js and Phantom js
我正在寻找抓取页面作为学习 phantomjs 的练习,但是我目前遇到了问题。图像加载被推迟,所以我想弄清楚如何让 phantom js 向下滚动并等待图像加载。滚动到页面底部不起作用,所以我想每 3 秒滚动 100px,直到它到达页面底部。我将如何实现这一目标?
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();
})();
您也可以使用基于 phantom.js 的 node-webshot 来呈现 pdf。它有很多配置。你需要的是 renderDelay 来延迟屏幕截图和 shotOffset 来滚动你想要的地方。
PhantomJS 确实支持 "scrolling",有一个页面 属性 scrollPosition
大概可以这样使用:
await page.property('scrollPosition', { top: 300, left: 0 });
您可以动态更改 scrollPosition
,随时间增加它,这应该会触发负责图像加载的回调。
这里是 an example 原始 PhantomJS 脚本,展示了沿 Twitter 时间线发展的技术。
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;
};
我正在寻找抓取页面作为学习 phantomjs 的练习,但是我目前遇到了问题。图像加载被推迟,所以我想弄清楚如何让 phantom js 向下滚动并等待图像加载。滚动到页面底部不起作用,所以我想每 3 秒滚动 100px,直到它到达页面底部。我将如何实现这一目标?
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();
})();
您也可以使用基于 phantom.js 的 node-webshot 来呈现 pdf。它有很多配置。你需要的是 renderDelay 来延迟屏幕截图和 shotOffset 来滚动你想要的地方。
PhantomJS 确实支持 "scrolling",有一个页面 属性 scrollPosition
大概可以这样使用:
await page.property('scrollPosition', { top: 300, left: 0 });
您可以动态更改 scrollPosition
,随时间增加它,这应该会触发负责图像加载的回调。
这里是 an example 原始 PhantomJS 脚本,展示了沿 Twitter 时间线发展的技术。
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;
};