2013-06-28 18 views
0

好的,所以這是OSX特有的R問題。R for OSX中的問題與download.file函數

我試圖通過API下載XML數據。下面的代碼在PC上運行得很好,但不是在Mac上。我旋轉了所有的「方法」(捲曲等)無濟於事。有什麼想法嗎?

tempx <- "temp.xml" 
url <- "http://usaspending.gov/fpds/fpds.php?detail=b&fiscal_year=2012&maj_agency_cat=97&max_records=10&sortby=d&records_from=1" 

download.file(url, tempx, method="auto") 

ETA:這是我的錯誤:

trying URL 'http://usaspending.gov/fpds/fpds.php?detail=b&fiscal_year=2012&maj_agency_cat=97&max_records=10&sortby=d&records_from=1' 
Error in download.file(url, tempx, method = "auto") : 
    cannot open URL 'http://usaspending.gov/fpds/fpds.php?detail=b&fiscal_year=2012&maj_agency_cat=97&max_records=10&sortby=d&records_from=1' 
+0

你怎麼得到的錯誤? – agstudy

+0

查看包XML解析XML和RCurl捲曲 –

回答

1

這正常工作與httr

library(httr) 
url <- "http://usaspending.gov/fpds/fpds.php?detail=b&fiscal_year=2012&maj_agency_cat=97&max_records=10&sortby=d&records_from=1" 
GET(url) 

,因爲它會自動處理重定向:

GET(url)$url 
# [1] "http://usaspending.gov/api/fpds_api_basic.php?fiscal_year=2012&maj_contracting_agency=97%2A&Contracts=c&sortby=SIGNED_DATE%2Basc&records_from=0&max_records=10&sortby=SIGNED_DATE+asc" 
+0

然後如何從結果中提取XML文本? –

+0

沒關係!似乎xmlTreeParse在GET分配的對象上工作得很好。謝謝!!! –

0

這與其說是一個答案,問心無愧格式的註釋。我也用蘋果OSX系統,並與你的代碼相同的問題,以及我在努力解決的問題:

library(RCurl) 
library(XML) 
gotten <- getURL("http://usaspending.gov/fpds/fpds.php?detail=b&fiscal_year=2012&maj_agency_cat=97&max_records=10&sortby=d&records_from=1") 

> gotten 
[1] "\n" 


> gotten2 <- getURLContent("http://usaspending.gov/fpds/fpds.php?detail=b&fiscal_year=2012&maj_agency_cat=97&max_records=10&sortby=d&records_from=1") 
> 
> gotten2 
[1] "\n" 
attr(,"Content-Type") 

"text/xml" 

所以我覺得某種反應的發生,但是,最初的反應是很短,該代碼還沒有準備好接受之後發生的事情。