我目前正在尝试使用CasperJS和PhantomJS(两个非常出色的工具,感谢n1k0和Ariya)来爬取Google关键字工具,但我无法使其正常工作。
这是我的当前流程:
- 使用我的Google帐户登录(以避免在关键字工具中遇到验证码)。
- 导航到关键字工具页面。
- 填写搜索表单并按下
Search
按钮。
我卡在第3步:搜索表单不是普通的HTML表单,因此我无法使用Casper#fill()
。相反,我直接访问字段。以下是我尝试更改Word or phrase
字段值的一些语法:
this.evaluate(function() {
// Trying to change the value...
document.querySelector('textarea.sP3.sBFB').value = 'MY SUPER KEYWORDS';
document.querySelector('textarea.sP3.sBFB').setAttribute('value', 'MY SUPER KEYWORDS');
document.querySelector('textarea').value = 'MY SUPER KEYWORDS'; // there's only one <textarea> on the page
// Trying to change other attributes...
document.querySelector('textarea.sP3.sBFB').textContent = 'MY SUPER KEYWORDS';
document.querySelector('textarea').style.backgroundColor = 'yellow';
});
一切都不起作用。我在Casper#capture()
后面执行,以查看字段包含什么内容。如您所见,它确认我在正确的页面上并且已登录,但是<textarea>
为空。
奇怪的是,我可以访问DOM的其他部分:通过执行以下操作,我可以更改一个链接的文本,该链接说高级选项和过滤器
为___VINCE SAYS HELLO___
(请参见截图):
this.evaluate(function() {
document.querySelector('a.sLAB').textContent = '___VINCE SAYS HELLO___';
});
顺便说一句,我知道爬取Google关键词工具是违反服务条款的,但我认为这个问题可能会引起任何试图爬取JavaScript/Ajax重载站点的人的兴趣。
WaitFor()
语句中的。我尝试了一个WaitForSelector('textarea.sP3.sBFB')
和一个使用__utils__.exists()
测试字段存在性的函数的WaitFor()
,但都没有起作用。 - AngularChef