gpt4 book ai didi

xml - URL 被 httr::GET 与 xmlParse 截断

转载 作者:数据小太阳 更新时间:2023-10-29 02:23:44 25 4
gpt4 key购买 nike

我正在尝试使用两种不同的方法(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"

有趣的是:如果我们检查所请求 URL 的 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"

所以在某些时候我的查询词被截断了。如果手动将整个请求放入浏览器,它会按预期运行。

如有任何解决此问题的建议,我们将不胜感激。

最佳答案

您可以尝试用 + 替换您的 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

关于xml - URL 被 httr::GET 与 xmlParse 截断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28780985/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com