开始使用免费开始使用

添加自定义 user agent

其实,httpbin.org 还提供了一个仅返回当前 user agent 的地址(https://httpbin.org/user-agent)。本练习中,您将使用它,并修改自己的 user agent,使其对您正在抓取的网站所有者来说更有意义。

正如您在视频中所见,在使用 httr 获取网络资源时,有两种方式自定义 user agent:

  1. 局部设置,即作为当前请求方法的参数。
  2. 通过 set_config() 全局设置。

本练习是课程的一部分

R 语言网页抓取

查看课程

交互式实操练习

通过完成这段示例代码来试试这个练习。

# Pass a custom user agent to a GET query to the mentioned URL
response <- ___
# Print the response content
___(response)
编辑并运行代码