我正在尝试 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/