【发布时间】:2010-12-27 05:50:49
【问题描述】:
如何在 R 中更改 http 请求中的用户代理字符串?以及如何确定我当前的用户代理字符串是什么样的?
提前致谢。
【问题讨论】:
标签: url r httpwebrequest http-headers user-agent
如何在 R 中更改 http 请求中的用户代理字符串?以及如何确定我当前的用户代理字符串是什么样的?
提前致谢。
【问题讨论】:
标签: url r httpwebrequest http-headers user-agent
options("HTTPUserAgent") 或 getOption("HTTPUserAgent") 打印您当前的设置,options(HTTPUserAgent="My settings") 是更改它的方法。
要临时更改此选项,请使用:withr::with_options:
withr::with_options(list(HTTPUserAgent="My settings"), download.file(..something..))
或Droplet answer,如果您使用download.file。
【讨论】:
在接受的答案中使用options() 的解决方案将全局更改整个会话的设置(除非您将其更改回来)。
要在download.file() 发出的请求中临时更改用户代理,您需要使用headers 参数:
download.file("https://httpbin.org/user-agent",
destfile = "test.txt",
headers = c("User-Agent" = "My Custom User Agent"))
从 R 4.0.0 开始,您还可以在 available.packages() 和 install.packages() 中使用此参数,并将其转发到 download.file()。
【讨论】: