节点js和幻影js [英] Node js and Phantom js

查看:88
本文介绍了节点js和幻影js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望抓取页面作为学习phantomjs的练习,但是目前遇到问题.图像加载被推迟了,所以我试图弄清楚如何让幻影js向下滚动并等待图像加载.滚动到页面底部不起作用,因此我考虑每3秒滚动100像素,直到到达页面底部.我将如何实现呢?

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();
})();

解决方案

PhantomJS确实支持滚动",有一个页面属性scrollPosition可能可以这样使用:

await page.property('scrollPosition', { top: 300, left: 0 });

您可以动态更改scrollPosition,并在一定时间内增加它,这将触发负责图像加载的回调.

在原始的PhantomJS脚本中,有一个示例,显示了沿Twitter时间轴移动的技术. /p>

I'm looking to scrape a page as an exercise to learn phantomjs however I'm having an issue currently. The image loading is deferred so I'm trying to figure out how I can get phantom js to scroll down and wait for the images to load. SCrolling to the bottom of the page doesnt work so I was thinking of scrolling 100px every 3 seconds until it gets to the bottom of the page. How would I achieve this with?

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();
})();

解决方案

PhantomJS does support "scrolling", there is a page property scrollPosition which can probably used like this:

await page.property('scrollPosition', { top: 300, left: 0 });

You can change scrollPosition dynamically, increasing it within time, which should trigger callbacks responsible for image load.

Here's an example in raw PhantomJS script showing the technique to go down Twitter's timeline.

这篇关于节点js和幻影js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆