開始使用免費開始

加入自訂 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)
編輯並執行程式碼