開始使用免費開始

將超連結解析成資料框

請看看下面這個含有「helpful links」的 ul 清單。

它包含 3 個 li 元素,而每個 li 內都有帶連結的 a 元素:


Helpful links

Compiled with help from Google.


對應的 HTML 程式碼已作為字串放在 hyperlink_raw_html 中。

在這個練習中,你要把這些連結解析成一個 R 資料框,方法是只選取位於 li 元素裡的 a 元素。

補充:你會使用 Tidyverse 的 tibble()tibble() 基本上是精簡版的 data.frame(),你應該已經很熟悉了。和 data.frame() 一樣,你以欄名與資料成對的方式指定欄位名稱與值,例如:

my_tibble <- tibble(
  column_name_1 = value_1,
  column_name_2 = value_2,
  ...
)

本練習屬於課程

R 的網頁爬蟲

檢視課程

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Extract all the a nodes from the bulleted list
links <- hyperlink_raw_html %>% 
  read_html() %>% 
  html_elements('li ___')
編輯並執行程式碼