我正在尝试从this government website的API自动下载一些数据。
网站指示:
All requests for this version should be made to the following URL:
http://api.finder.healthcare.gov/v2.0/
我可以找到有关如何发送xml请求的大量信息,但没有一个示例是R
- 具体..并且有很多R代码可以显示如何使用XML
,httr
和RCurl
包,但我在SO或r-help邮件列表上找不到任何关于如何发送 xml请求的示例。 ..还有更多用于解析响应的文档。
在the government website上,如果单击PlansForIndividualOrFamily Samples
示例,则会显示需要发送的xml请求(下面的代码)。
url <- "http://api.finder.healthcare.gov/v2.0/"
xml.request <-
"<?xml version='1.0' encoding='UTF-8'?>
<PrivateOptionsAPIRequest>
<PlansForIndividualOrFamilyRequest>
<Enrollees>
<Primary>
<DateOfBirth>1990-01-01</DateOfBirth>
<Gender>Male</Gender>
<TobaccoUser>Smoker</TobaccoUser>
</Primary>
</Enrollees>
<Location>
<ZipCode>69201</ZipCode>
<County>
<CountyName>CHERRY</CountyName>
<StateCode>NE</StateCode>
</County>
</Location>
<InsuranceEffectiveDate>2012-10-01</InsuranceEffectiveDate>
<IsFilterAnalysisRequiredIndicator>false</IsFilterAnalysisRequiredIndicator>
<PaginationInformation>
<PageNumber>1</PageNumber>
<PageSize>10</PageSize>
</PaginationInformation>
<SortOrder>
<SortField>OOP LIMIT - INDIVIDUAL - IN NETWORK</SortField>
<SortDirection>ASC</SortDirection>
</SortOrder>
<Filter/>
</PlansForIndividualOrFamilyRequest>
</PrivateOptionsAPIRequest>"
答案 0 :(得分:9)
使用RCurl
执行此操作;
myheader=c(Connection="close",
'Content-Type' = "application/xml",
'Content-length' =nchar(xml.request))
data = getURL(url = url,
postfields=xml.request,
httpheader=myheader,
verbose=TRUE)
就是这样。然后,您可以使用xpathApply
XML包来检索数据。例如,要获得家庭ID:
library(XML)
xmltext <- xmlTreeParse(data, asText = TRUE,useInternalNodes=T)
unlist(xpathApply(xmltext,'//Plan/PlanID',xmlValue)) ## change the right xpath here
"29678NE0780012" "29678NE0780011" "29678NE0140010"
"29678NE0780010" "29678NE0140019" "29678NE0140018" "29678NE0140017"
"29678NE0140016" "29678NE0780005" "29678NE0780004"