使用 Node.js 进行网页抓取时,我可以在页面上运行所有 JavaScript 吗?(即模拟真实浏览器?) [英] When web scraping with Node.js, can I run all JavaScripts on the page? (i.e., simulate a real browser?)

查看:56
本文介绍了使用 Node.js 进行网页抓取时,我可以在页面上运行所有 JavaScript 吗?(即模拟真实浏览器?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 node.js 进行一些网页抓取.使用 jsdom,很容易加载 DOM 并将 JavaScript 注入其中.我想更进一步:运行从网页链接到的所有 JavaScript,然后检查生成的 DOM,包括元素的视觉属性(高度、宽度等).

I'm trying to do some web scraping with node.js. Using jsdom, it is easy to load up the DOM and inject JavaScript into it. I want to go one step further: run all JavaScript linked to from the web page and then inspect the resulting DOM, including visual properties (height, width, etc) of elements.

到目前为止,当我尝试使用 jsdom 检查 DOM 元素的维度时,我得到了 NaN.

Thus far, I get NaN when I try to inspect the dimensions of DOM elements with jsdom.

这可能吗?

我觉得有两个不同的挑战:

It strikes me that there are two distinct challenges:

  1. 在网页上运行所有 JS
  2. 除了DOM之外,还让Node模拟窗口/屏幕渲染

提出问题的另一种方式:是否可以将 node.js 用作可以编写脚本的完全无头浏览器?

Another way to ask the question: is it possible to use node.js as a completely headless browser that you can script?

如果这是不可能的,是否有人对我可以使用什么库来执行此操作有任何建议?我的语言相对不可知.

If this isn't possible, does anyone have suggestions for what library I can use to do this? I'm relatively language agnostic.

推荐答案

看看 PhantomJS.使用起来非常简单.

Take a look at PhantomJS. Incredibly simple to use.

http://www.phantomjs.org/

PhantomJS 是一个命令行工具,用于打包和嵌入 WebKit.从字面上看,它就像任何其他基于 WebKit 的 Web 浏览器一样,除了屏幕上没有显示任何内容(因此,术语无头).除此之外,PhantomJS 还可以使用其 JavaScript API 进行控制或编写脚本.

PhantomJS is a command-line tool that packs and embeds WebKit. Literally it acts like any other WebKit-based web browser, except that nothing gets displayed to the screen (thus, the term headless). In addition to that, PhantomJS can be controlled or scripted using its JavaScript API.

这篇关于使用 Node.js 进行网页抓取时,我可以在页面上运行所有 JavaScript 吗?(即模拟真实浏览器?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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