我试图用两个不同的方法(xmlParse和httr::GET)请求一个XML文档,并期望响应是相同的。我在xmlParse中得到的响应是我所期望的,但是httr::GET请求URL在某个时候会被截断。
举个例子:
require(httr)
require(XML)
require(rvest)
term <- "alopecia areata"
request <- paste0("http://eutils.ncbi.nlm.nih.gov/entrez/eutils/egquery.fcgi?term=",term)
#requesting URL with XML
xml_response <- xmlParse(request)
xml_response %>%
xml_nodes(xpath = "//Result/Term") %>%
xml_text 这是它应该返回的。
[1] "alopecia areata" 现在是httr
httr_response <- GET(request)
httr_content <- content(httr_response)
httr_content %>%
xml_nodes(xpath = "//Result/Term") %>%
xml_text 这会返回
[1] "alopecia"有趣的是:如果我们检查请求的httr_response元素,它是正确的。只有反应是错误的。
> httr_response$request$opts$url
[1] "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/egquery.fcgi?term=alopecia areata"
> httr_response$url
[1] "http://eutils.ncbi.nlm.nih.gov/gquery?term=alopecia&retmode=xml"因此,在某个时候,我的查询项被截断了。如果整个请求是手动放到浏览器中的,那么它的行为就像预期的那样。
如能提出解决这一问题的任何建议,将不胜感激。
发布于 2015-02-28 11:58:34
您可以尝试用+替换URL中的空格,以防止它被截断:
httr_response <- GET(gsub(" ","+",request))
httr_content <- content(httr_response)
httr_content %>%
xml_nodes(xpath = "//Result/Term") %>%
xml_text
#[1] "alopecia areata"关于空格和URL的更多信息here
https://stackoverflow.com/questions/28780985
复制相似问题