我正在尝试用 aspx 抓取一个网站,该网站有一个 js dopostBack 按钮。其中一个按钮是回复我的页面的可打印视图,另一个按钮将 .csv 文件推送到客户端。
我在这里看到一个描述 csv 下载问题但没有回答的问题:PhantomJS download using a javascript link
所以我专注于尝试在 phantomJs 中获取可打印视图,因为它看起来更简单(它显示在浏览器窗口中,一定有办法!)
按钮代码:
<a id="ctl00_ctl00_ctl00_MainContentPlaceHolder_PrintResultsLinkButton"
title="Print Results" class="btn-blue"
href="javascript:
__doPostBack('ctl00$ctl00$ctl00$MainContentPlaceHolder$PrintResultsLinkButton','')
">
<span>Print Results</span>
</a>
我可以单击 Phantom/casper 的链接,但它似乎不起作用。我认为必须在单击按钮时发送请求,并带有所有标题,但我不知道如何接收答案。帮助?
我的 casper 代码,工作到我应该得到结果页面的地步:https ://gist.github.com/xShirase/7156131
我也尝试过评估 js 函数,它在 chrome 控制台中工作,但仍然没有给我幻影中的结果......
迄今为止的最后一次尝试:我第一次加载我的页面,以获取 cookie 和隐藏输入的值,然后尝试自己发布请求。捕获中的输出仍然相同,所以我知道我的请求没问题,但为什么我没有得到正确的结果?
代码 :
casper.start();
capture = function(){
var url = 'http://www.cms.gov/apps/physician-fee-schedule/search/search-criteria.aspx';
casper.open(url).thenClick('a.btn',function() {
this.then(grabResults);
});
};
grabResults = function(){
this.echo(this.getCurrentUrl());
this.open('http://www.cms.gov/apps/physician-fee-schedule/search/search-results.aspx?Y=0&T=0&HT=2&CT=3&H1=00100&H2=11400&M=5').then(function(){
this.capture('page.png');
a = this.evaluate(function(){
var v = $('input:hidden#__VIEWSTATE').val();
var d = document.cookie;
return [v,d];
});
});
this.then(grabPRResults);
};
grabPRResults = function(){
this.open('http://www.cms.gov/apps/physician-fee-schedule/search/search-results.aspx?Y=0&T=0&HT=2&CT=3&H1=00100&H2=11400&M=5', {
method: 'post',
headers: {
'Host': 'www.cms.gov',
'Connection': 'keep-alive',
'Content-Length': '103902',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Origin': 'http://www.cms.gov',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36',
'Content-Type': 'application/x-www-form-urlencoded',
'Referer': 'http://www.cms.gov/apps/physician-fee-schedule/search/search-results.aspx?Y=0&T=0&HT=2&CT=3&H1=00100&H2=11400&M=5',
'Accept-Encoding': 'gzip,deflate,sdch',
'Accept-Language': 'fr,en-US;q=0.8,en;q=0.6',
'Cookie': a[1]
},
data: {
'__EVENTTARGET':'ctl00$ctl00$ctl00$CMSGMainContentPlaceHolder$ToolContentPlaceHolder$PFSSContentPlaceHolder$PrintResultsLinkButton',
'__EVENTARGUMENT':'',
'__LASTFOCUS':'',
'__VIEWSTATE':a[0],
'ctl00$ctl00$ctl00$CMSGMainContentPlaceHolder$ToolContentPlaceHolder$PFSSContentPlaceHolder$PFSSResultsCPEWrapper_ClientState':'false',
'ctl00$ctl00$ctl00$CMSGMainContentPlaceHolder$ToolContentPlaceHolder$PFSSContentPlaceHolder$YearDropDown':'2013',
'ctl00$ctl00$ctl00$CMSGMainContentPlaceHolder$ToolContentPlaceHolder$PFSSContentPlaceHolder$TypeOfInfoDropDown':'pi',
'ctl00$ctl00$ctl00$CMSGMainContentPlaceHolder$ToolContentPlaceHolder$PFSSContentPlaceHolder$HCPCTypeDropDown':'range',
'ctl00$ctl00$ctl00$CMSGMainContentPlaceHolder$ToolContentPlaceHolder$PFSSContentPlaceHolder$CarrierTypeDropDown':'all',
'ctl00$ctl00$ctl00$CMSGMainContentPlaceHolder$ToolContentPlaceHolder$PFSSContentPlaceHolder$HCPC1Textbox':'00100',
'ctl00$ctl00$ctl00$CMSGMainContentPlaceHolder$ToolContentPlaceHolder$PFSSContentPlaceHolder$HCPC2Textbox':'11400',
'ctl00$ctl00$ctl00$CMSGMainContentPlaceHolder$ToolContentPlaceHolder$PFSSContentPlaceHolder$HCPC3Textbox':'',
'ctl00$ctl00$ctl00$CMSGMainContentPlaceHolder$ToolContentPlaceHolder$PFSSContentPlaceHolder$HCPC4Textbox':'',
'ctl00$ctl00$ctl00$CMSGMainContentPlaceHolder$ToolContentPlaceHolder$PFSSContentPlaceHolder$HCPC5Textbox':'',
'ctl00$ctl00$ctl00$CMSGMainContentPlaceHolder$ToolContentPlaceHolder$PFSSContentPlaceHolder$ModifierDropDown':'%',
'ctl00$ctl00$ctl00$CMSGMainContentPlaceHolder$ToolContentPlaceHolder$PFSSContentPlaceHolder$CarrierDropDown':'default',
'ctl00$ctl00$ctl00$CMSGMainContentPlaceHolder$ToolContentPlaceHolder$PFSSContentPlaceHolder$CarrierLocalityDropDown':'default',
'ctl00$ctl00$ctl00$CMSGMainContentPlaceHolder$ToolContentPlaceHolder$PFSSContentPlaceHolder$ResultsControl1$PFSSGridView$ctl01$ tbGotoPage':'',
'ctl00$ctl00$ctl00$CMSGMainContentPlaceHolder$ToolContentPlaceHolder$PFSSContentPlaceHolder$ResultsControl1$PFSSGridView$ctl01$PFSSGridViewtopddlTopPageSize':'10',
'ctl00$ctl00$ctl00$CMSGMainContentPlaceHolder$ToolContentPlaceHolder$PFSSContentPlaceHolder$ResultsControl1$PFSSGridView$ctl14$ tbGotoPageBottom':'',
'ctl00$ctl00$ctl00$CMSGMainContentPlaceHolder$ToolContentPlaceHolder$PFSSContentPlaceHolder$ResultsControl1$PFSSGridView$ctl14$PFSSGridViewbottomddlBottomPageSize':'10',
'ctl00$ctl00$ctl00$CMSGMainContentPlaceHolder$ToolContentPlaceHolder$PFSSContentPlaceHolder$DownloadsWidget1$DownloadsCPEWrapper_ClientState':'false'
}
}).then(function(){
this.wait(25000);
this.then(lest);
});
};
lest= function(){
this.capture('ppp.png');
};
casper.then(capture);
casper.run();