Web Scraping Wikipedia - 字符串操作

Web Scraping Wikipedia - string manipulation

我设法抓取了这个维基百科页面 Oscars Nominations 并提取了 table 下的 "Nominees"。我可以通过下面的代码得到 table:

wiki <- "https://en.wikipedia.org/wiki/89th_Academy_Awards"
text <- wiki %>% 
         read_html() %>% 
         html_nodes('//*[@id="mw-content-text"]/table[3]') %>% 
         html_table()

输出一个'list'作为名字'text'

test <- data.frame(one=unlist(text), stringsAsFactors=F)
row.names(test) <- NULL
test <- test[-16,]
nw_lst <- strsplit(test, "\n")

我尝试将结果放在一个 df 中,然后删除一个无用的行,然后 'strsplit' 在 'nw_lst' 中的换行符正则表达式 '\n' 上输出另一个列表但很多具有 23 个元素的清洁器,对应于下面列出的每项奥斯卡提名。然后我想将列表解析为 2 个 df,一个用于最佳影片提名,第二个 df 与其他提名。

oscr.bp <- data.frame(Best.Picture=unlist(nw_lst[[1]]), stringsAsFactors=F)
oscr.bp <- as.data.frame(oscr.bp[-1,], stringsAsFactors=F)
colnames(oscr.bp) <- c("Best.Picture")

所以这是我的问题,一旦我将提名分开,我想清理文本。问题是由于某种原因 'stringr' 包中的任何内容都无法删除除电影标题之外的所有不必要的文本。

str_replace_all(oscr.bp$Best.Picture,pattern = "\n", replacement = " ") 
str_replace_all(oscr.bp$Best.Picture,pattern = "[\^]", replacement = " ") 
str_replace_all(oscr.bp$Best.Picture,pattern = "\"", replacement = " ") 
str_replace_all(oscr.bp$Best.Picture,pattern = "\s+", replacement = " ") 
str_trim(oscr.bp$Best.Picture,side = "both")

但是当我在我的环境中检查 df 的结构并单击蓝色箭头以查看向量 类 并将鼠标悬停在 chr 向量上时,但它在字符向量中有奇怪的形状并且有这个 |__truncated__ 在字符串中,但在控制台中检查字符串时不可见。

我只想知道清理这些字符串的最佳方法,或者另一种方法来仅获取 <ul> 和 [=16] 下 HTML 节点中每个提名的标题名称=]解析?除了查看源代码并使用选择器小工具找到我需要的内容外,对基本 HTML 代码含义了解不多。

我相信我有解决问题的方法,但编码问题可能仍然存在。实际任务是简单地获取电影标题,其后是破折号。

我从粘贴您的代码开始,除了指定 html_nodes 参数是 xpath 而不是 css(在您的问题中为我抛出错误)。

wiki <- "https://en.wikipedia.org/wiki/89th_Academy_Awards"
text <- wiki %>% 
         read_html() %>% 
         html_nodes(xpath='//*[@id="mw-content-text"]/table[3]') %>% 
         html_table()

那你定义我就停了Best.Picture。将它强制转换为 data.frame 是不必要的,除非我遗漏了什么,因为它只是一个向量。

Best.Picture <- unlist(nw_lst[[1]])[-1]

然后我拆分 Best.Picture 字符向量中的每个条目,并应用于拆分列表(每个元素都是一个向量,将每个向量元素中的每个字符隔离开来)。我们这样做是为了确定魔术破折号的位置(我只是直接从终端复制并粘贴,因为破折号不是“-”,而是一些类似的符号(这可能说明评论中提到的编码问题。

dash <- sapply(strsplit(Best.Picture, ''), function(x){which(x == '–')})

确定破折号在 Best.Picture 元素的每个元素中的位置后,我们可以使用 substr 将向量截断为我们关心的部分。如果您想安全起见,可以将所有内容剪切到破折号 - 1(这也会剪切破折号),然后使用 trimws 删除前导或尾随空格。

movTitle <- substr(Best.Picture, 1, dash-2)

另一种方法是针对每个人<td>,然后使用可用的元数据:

library(rvest)
library(tidyverse)

pg <- read_html("https://en.wikipedia.org/wiki/89th_Academy_Awards")

html_nodes(pg, xpath=".//h2[span/@id = 'Nominees']/following-sibling::table[1]") %>%
  html_nodes("td") %>%
  map_df(function(x) {
    category <- html_nodes(x, "div") %>% html_text()
    html_nodes(x, "li") %>%
      map_df(function(y) {
        html_nodes(y, "a") %>% html_attr("title") -> tmp
        movie <- tmp[1]
        nominee <- tmp[-1]
        data_frame(movie=rep(movie, length(nominee)), nominee)
      }) %>%
      mutate(category = category)
  }) %>%
  select(category, movie, nominee)
## # A tibble: 236 × 3
##        category          movie           nominee
##           <chr>          <chr>             <chr>
## 1  Best Picture Arrival (film)        Shawn Levy
## 2  Best Picture Arrival (film)       David Linde
## 3  Best Picture  Fences (film)       Scott Rudin
## 4  Best Picture  Fences (film) Denzel Washington
## 5  Best Picture  Fences (film)        Todd Black
## 6  Best Picture  Hacksaw Ridge     Bill Mechanic
## 7  Best Picture  Hacksaw Ridge      David Permut
## 8  Best Picture Hidden Figures   Donna Gigliotti
## 9  Best Picture Hidden Figures     Peter Chernin
## 10 Best Picture Hidden Figures     Jenno Topping
## # ... with 226 more rows