我試圖使用rvestR 中的包從 wiki fandom 網站提取資料。但是,我遇到了幾個問題,因為資訊框不是 HTML 表格。請參閱下文,了解我處理此問題的嘗試:
library(tidyverse)
library(data.table)
library(rvest)
library(httr)
url <- c("https://starwars.fandom.com/wiki/Anakin_Skywalker")
#See here that the infobox information does not appear when checking for HTML tables in the page
df <- read_html(url) %>%
html_table()
#So now just extract data using the CSS selector
df <- read_html(url) %>%
html_element("aside")
html_text2()
第二次嘗試確實成功地提取了原始資料,但它的格式化方式不容易格式化為干凈的資料幀。所以,然后我嘗試單獨提取表的每個元素,這可能更容易清理和構造成資料框。但是,當我嘗試使用 XPath 執行此操作時,我得到一個空結果:
df <- read_html(url) %>%
html_nodes(xpath = '//*[@id="mw-content-text"]/div/aside/section[1]') %>%
html_text2()
所以我想我的問題主要是:有沒有人知道自動提取資料框友好格式的資訊框的好方法?如果沒有,有人能指出我為什么單獨提取每個面板的嘗試不起作用嗎?
uj5u.com熱心網友回復:
如果您div.pi-data直接定位,您可以執行以下操作:
bind_rows(
read_html(url) %>%
rvest::html_nodes("div.pi-data") %>%
map(.f = ~tibble(
label = html_elements(.x, ".pi-data-label") %>% html_text2(),
text= html_elements(.x, ".pi-data-value") %>% html_text2() %>% strsplit(split="\n")
) %>% unnest(text)
)
)
輸出:
# A tibble: 29 x 2
label text
<chr> <chr>
1 Homeworld Tatooine[1]
2 Born 41 BBY,[2] Tatooine[3]
3 Died 4 ABY,[4]DS-2 Death Star II Mobile Battle Station, Endor system[5]
4 Species Human[1]
5 Gender Male[1]
6 Height 1.88 meters,[1] later 2.03 meters (6 ft, 8 in) in armor[6]
7 Mass 120 kilograms in armor[7]
8 Hair color Blond,[8] light[9] and dark[10]
9 Eye color Blue,[11] later yellow (dark side)[12]
10 Skin color Light,[11] later pale[5]
# ... with 19 more rows
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/478745.html
