Phantomjs 上的 javascriptEnabled 问题

发布于 2024-12-13 02:57:18 字数 313 浏览 1 评论 0原文

我正在使用 phantomjs 从网站上抓取一些数据。为了加快加载速度,我通过这样做禁用了网页上js的执行:

page.settings.javascriptEnabled = false;

但这会导致问题——它使 page.evaluate(somefunc.toString()) 返回 null 无论函数应该返回什么。 如果我将 js 设置默认设置为 true,则 page.evaluate() 将再次工作。

我应该如何解决这个问题? 谢谢

I was using phantomjs to scrape some data from a website. In order to speed up the loading I disabled the execution of js on the webpage by doing this:

page.settings.javascriptEnabled = false;

but this causes the problem--it makes page.evaluate(somefunc.toString()) return null no matter what the function should return.
If I keep the js setting default to true the page.evaluate() would work again.

How should I work around this?
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一瞬间的火花 2024-12-20 02:57:19

一般来说,不要这样做。

但是,如果您确实需要,您可以保持JavaScript启用,但阻止加载外部.js文件。例如:

page.onResourceRequested = function(requestData, request) {
  if (requestData['Content-Type'] == 'application/javascript' || requestData['Content-Type'] == 'text/javascript') {
    console.log('Disabling JavaScript files. Aborting: ' + requestData['url']);
    request.abort();
  }
};

In general, don't do that.

However, if you really need that, you may keep JavaScript enabled, but block loading external .js files. E.g.:

page.onResourceRequested = function(requestData, request) {
  if (requestData['Content-Type'] == 'application/javascript' || requestData['Content-Type'] == 'text/javascript') {
    console.log('Disabling JavaScript files. Aborting: ' + requestData['url']);
    request.abort();
  }
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文