使用javascript链接下载PhantomJS

Duk*_*ver 13 javascript screen-scraping phantomjs

我试图刮掉以下网站:

http://www.fangraphs.com/leaders.aspx?pos=all&stats=bat&lg=all&qual=0&type=8&season=2011&month=0&season1=2011&ind=0&team=0&rost=0&players=0

如果单击标题为"导出数据"的表右上角的小按钮,则会运行javascript脚本,我的浏览器会以.csv格式下载该文件.我希望能够编写一个可以自动执行此操作的PhantomJS脚本.有任何想法吗?

上面的按钮被编码为HTML,如下所示:

<a id="LB_cmdCSV" href="javascript:__doPostBack('LB$cmdCSV','')">Export Data</a></div>
Run Code Online (Sandbox Code Playgroud)

我还在HTML源代码中找到了这个函数:

<script type="text/javascript">
//<![CDATA[
var theForm = document.forms['form1'];
if (!theForm) {
    theForm = document.form1;
}
function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}
//]]>
</script>
Run Code Online (Sandbox Code Playgroud)

我是PhantomJS/Javascript的新手,可以在这里使用一些指针.我想我已经找到了我需要自动完成的所有信息(如果我错了,请纠正我),但不知道从哪里开始编码.谢谢你的帮助.

编辑 - 这就是我的脚本现在的样子:

var page = new WebPage();
url = 'http://www.fangraphs.com/leaders.aspx?pos=all&stats=bat&lg=all&qual=0&type=8&season=2011&month=0&season1=2011&ind=0&team=0&rost=0& players=0';

page.open(encodeURI(url), function (status){
  if (status !== "success") {
    console.log("Unable to access website");
  } else {
      page.evaluate(function() {
        __doPostBack('LB$cmdCSV', '');
      });
    }
  phantom.exit(0);
});
Run Code Online (Sandbox Code Playgroud)

Cam*_*ker 0

__doPostBack('LeaderBoard1$cmdCSV','');您不能在网页的上下文中运行代码吗?

像这样的东西:

page.evaluate(function() {
  __doPostBack('LeaderBoard1$cmdCSV','');
});
Run Code Online (Sandbox Code Playgroud)

我还没有在 PhantomJS 中测试过这段代码,但理论上它应该可以工作,因为从 Google Chrome 的开发者控制台运行 __doPostBack 方法是有效的。如果对在 PhantomJS 中运行 JavaScript 代码有疑问,Google Chrome 的开发者控制台是测试代码的好方法,因为它像 PhantomJS 一样在 WebKit 上运行。我希望这有帮助。