I will give you a quick and dirty way to get data. First, you can use Fiddler2 http://www.fiddler2.com/fiddler2/ to check the POST that your browser sends. This results in the following POST:
POST http://www.invescopowershares.com/products/holdings.aspx?ticker=PGX HTTP/1.1 Host: www.invescopowershares.com User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:13.0) Gecko/20100101 Firefox/13.0 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: en-us,en;q=0.5 Accept-Encoding: gzip, deflate DNT: 1 Connection: keep-alive Referer: http://www.invescopowershares.com/products/holdings.aspx?ticker=PGX Content-Type: application/x-www-form-urlencoded Content-Length: 70669 __EVENTTARGET=ctl00%24MainPageLeft%24MainPageContent%24ExportHoldings1%24LinkButton1&__EVENTARGUMENT=&__VIEWSTATE=%2FwEPDwUKLTE1OTcxNjYzNw9kFgJmD2QWBAIDD2QWBAIDD2QWCAIBDw9kFgQeC2........
So, we see that 3 parameters are sent exactly __EVENTTARGET, __EVENTVALIDATION and __VIEWSTATE.
Required form to call postForm:
postForm(ftarget, "form name" = "aspnetForm", "method" = "POST", "action" = "holdings.aspx?ticker=PGX", "id" = "aspnetForm","__EVENTTARGET"=event.target,"__EVENTVALIDATION"=event.val,"__VIEWSTATE"=view.state)
Now comes a quick and dirty bit. I would just open the browser and get the relevant parameters, which it receives as follows:
library(rcom) ie = comCreateObject('InternetExplorer.Application') ie[["visible"]]=T # true for debugging ie$Navigate2("http://www.invescopowershares.com/products/holdings.aspx?ticker=PGX") while(comGetProperty(ie,"busy")||comGetProperty(ie,"ReadyState")<4){ Sys.sleep(1) print(comGetProperty(ie,"ReadyState")) } myDoc<-comGetProperty(ie,"Document") myPW<-comGetProperty(myDoc,"parentWindow") comInvoke(myPW,"execScript","var dumVar1=theForm.__EVENTVALIDATION.value;var dumVar2=theForm.__VIEWSTATE.value;","JavaScript") event.val<-myPW[["dumVar1"]] view.state<-myPW[["dumVar2"]] event.target<-"ctl00$MainPageLeft$MainPageContent$ExportHoldings1$LinkButton1" ie$Quit() ftarget<-"http://www.invescopowershares.com/products/holdings.aspx?ticker=PGX" web.data<-postForm(ftarget, "form name" = "aspnetForm", "method" = "POST", "action" = "holdings.aspx?ticker=PGX", "id" = "aspnetForm","__EVENTTARGET"=event.target,"__EVENTVALIDATION"=event.val,"__VIEWSTATE"=view.state) write(web.data[1],'temp.csv') fin.data<-read.csv('temp.csv') > fin.data[1,] ticker SecurityNum Name CouponRate maturitydate 1 PGX 949746879 WELLS FARGO & COMPANY PFD 0.08 rating Shares PercentageOfFund PositionDate 1 BBB+/Baa3 2538656 0.04442112 06/11/2012
__ EVENTVALIDATION, __VIEWSTATE may always be the same, or perhaps session cookies. You could probably get them using RCurl, but, as I said, this is a quick and dirty solution, and we just take the ones provided by Internet Explorer. What should be noted:
one). This requires windows with IE installed to use the rcom bit.
2). If you are using ie9, you may need to add invescopowershares.com to your compatibility view settings (since Microsoft seems to have blocked the call to event.val <-myPW [["dumVar1"]] like com)
EDIT (UPDATE)
Looking at the website in more detail __EVENTVALIDATION, __VIEWSTATE are set as javascript variables on the start page. We can simply parse them in a quick and dirty way as follows without resorting to calling the browser.
dum<-getURL("http://www.invescopowershares.com/products/holdings.aspx?ticker=PGX") event.target<-"ctl00$MainPageLeft$MainPageContent$ExportHoldings1$LinkButton1" event.val<-unlist(strsplit(dum,"__EVENTVALIDATION\" value=\""))[2] event.val<-unlist(strsplit(event.val,"\" />\r\n\r\n<script"))[1] view.state<-unlist(strsplit(dum,"id=\"__VIEWSTATE\" value=\""))[2] view.state<-unlist(strsplit(view.state,"\" />\r\n\r\n\r\n<script"))[1] ftarget<-"http://www.invescopowershares.com/products/holdings.aspx?ticker=PGX" web.data<-postForm(ftarget, "form name" = "aspnetForm", "method" = "POST", "action" = "holdings.aspx?ticker=PGX", "id" = "aspnetForm","__EVENTTARGET"=event.target,"__EVENTVALIDATION"=event.val,"__VIEWSTATE"=view.state) write(web.data[1],'temp.csv') fin.data<-read.csv('temp.csv')
The above should work with cross platform.