【问题标题】:Changing user agent string in a http request in R在 R 中的 http 请求中更改用户代理字符串
【发布时间】:2010-12-27 05:50:49
【问题描述】:

如何在 R 中更改 http 请求中的用户代理字符串?以及如何确定我当前的用户代理字符串是什么样的?

提前致谢。

【问题讨论】:

    标签: url r httpwebrequest http-headers user-agent


    【解决方案1】:

    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

    【讨论】:

      【解决方案2】:

      在接受的答案中使用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()

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-05-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-29
        相关资源
        最近更新 更多