jquery - 使用 PhantomJS 将 jQuery 包含到打开的页面中时,“null”不是对象错误

标签 jquery phantomjs

我正在尝试 PhantomJS并想用它从网页中提取内容。但是,我无法使用以下代码注入(inject) jQuery。

console.log('SSL support = ', require('system').isSSLSupported);

var page = require('webpage').create();
console.log('page created');
page.open('https://www.google.com/#q=my+test+query', function() {
  console.log('page opened');
  page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js", function() {
    console.log('jQuery injected');
    phantom.exit();
  });
});

当我运行代码时,我看到一个错误

C:\Users\royshi\SkyDrive\Developer\Crawler>phantomjs test.txt
SSL support =  true
page created
page opened
TypeError: 'null' is not an object (evaluating 'document.body.appendChild')

  http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js:1
^C
C:\Users\royshi\SkyDrive\Developer\Crawler>

我的代码与 the example provided by PhantomJS 没有太大不同。想知道这里可能出了什么问题。

编辑:

我在上面的示例中使用的 URL 似乎有问题(即“https://www.google.com/#q=my+test+query”)。该代码适用于另一个 URL:https://www.google.com/ :

C:\Users\royshi\SkyDrive\Developer\Crawler>phantomjs test.txt
SSL support =  true
page created
page opened
jQuery injected

C:\Users\royshi\SkyDrive\Developer\Crawler>

想知道 URL“https://www.google.com/#q=my+test+query”出了什么问题。

最佳答案

在获得解决方案时回答您的编辑:主题标签会触发重定向。由于我不了解 PhantomJS,我怀疑您需要手动遵循它,例如 this gist建议。

关于jquery - 使用 PhantomJS 将 jQuery 包含到打开的页面中时,“null”不是对象错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22899522/

相关文章:

gruntjs - 运行 jasmine 测试时 Grunt 超时

angular - 错误 : "component cannot define both template and templateUrl"

javascript - 幻影 Node 不传递 page.evaluate 函数的返回值

javascript - 多个脚本标签在我的页面中不起作用

php - 将值从 php 页面传递到其他页面的模式

javascript - 使用 require() 为服务器端/PhantomJS 转换 JS 脚本

json - NightmareJS 将变量保存到文件

javascript - 使用 jquery 在 li 标签的子 ul 中单击超链接标签时向正文添加一个类

JQuery ajax url 路径(相对于或绝对于什么?)

javascript 将小数点舍入到两位