我目前正在尝试使用CasperJS和PhantomJS(两者都是优秀的工具,感谢n1k0和Ariya)来抓取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()
,看看该字段包含的内容。 As you can see,它确认我在正确的页面上并且我已登录,但<textarea>
为空。
奇怪的是,我可以访问DOM的其他部分:我可以通过执行以下操作将Advanced Options and Filters
的链接文本更改为___VINCE SAYS HELLO___
(请参阅捕获):
this.evaluate(function() {
document.querySelector('a.sLAB').textContent = '___VINCE SAYS HELLO___';
});
PS。我知道抓取Google关键字工具是针对TOS的,但我认为这个问题可能会让任何试图抓住JavaScript / Ajax网站的人感兴趣。
答案 0 :(得分:1)
document.querySelector('textarea.sP3.sBFB').value = 'MY SUPER KEYWORDS';
您无法在textarea上使用elt.value
。你试过elt.textContent
吗?
答案 1 :(得分:1)
为什么要尝试刮取结果。谷歌已经为我们创建了一个csv文件。 尝试下载。链接选择器必须像$(&#39; .gux-combo gux-dropdown-c .sJK&#39;) 你会用它来自动化吗?
答案 2 :(得分:0)
我不确定这里到底发生了什么,但是你用于定位的课程对我来说是不同的。我假设您尝试定位的OneBoxKeywordsInputPanel-input
textarea有第二个类sPFB
,没有其他类。这些神秘的类可能在某种程度上是动态的。我建议使用更具描述性的类名。以下工作对我来说很好:
document.querySelector('textarea.OneBoxKeywordsInputPanel-input')