【问题标题】:Web Scraping a table that has inline pictures网页抓取具有内嵌图片的表格
【发布时间】:2021-05-12 07:18:18
【问题描述】:

我正在尝试将标题为 Battle Styles 的表刮到数据框中。 https://bulbapedia.bulbagarden.net/wiki/Battle_Styles_(TCG)#Set_lists

问题在于,许多行包含的图像具有重要信息,而 rvest 中没有提取这些信息。

表格应如下所示:

No.     Card name   Type    Rarity
001/163 Bellsprout  Grass   Common
002/163 Weepinbell  Grass   Uncommon
003/163 Victreebel  Grass   Rare
004/163 Cacnea      Grass   Common
005/163 Cacturne    Grass   Uncommon
006/163 KricketuneV Grass   Ultra-Rare Rare
007/163 Cherubi     Grass   Common
008/163 Cherrim     Grass   Rare Holo
009/163 Carnivine   Grass   Uncommon
010/163 Durant      Grass   Uncommon

如果我复制表格并将其粘贴到记事本中,我可以得到这个表格 ^^。

但是我的不包含图片中的任何信息。它看起来像这样:

     # A tibble: 184 x 6
   No.     Image `Card name` Type  Rarity Promotion
   <chr>   <lgl> <chr>       <chr> <lgl>  <chr>    
 1 001/163 NA    Bellsprout  ""    NA     Promotion
 2 002/163 NA    Weepinbell  ""    NA     Promotion
 3 003/163 NA    Victreebel  ""    NA     Promotion
 4 004/163 NA    Cacnea      ""    NA     Promotion
 5 005/163 NA    Cacturne    ""    NA     Promotion
 6 006/163 NA    Kricketune  ""    NA     Promotion
 7 007/163 NA    Cherubi     ""    NA     Promotion
 8 008/163 NA    Cherrim     ""    NA     Promotion
 9 009/163 NA    Carnivine   ""    NA     Promotion
10 010/163 NA    Durant      ""    NA     Promotion

图片所需的信息在替代文本中,所以我觉得解决方案应该是直截了当的,但我不知道如何得到它。

这是我的代码:

library(rvest)

BattlestylesURL <- "https://bulbapedia.bulbagarden.net/wiki/Battle_Styles_(TCG)"

temp <- BattlestylesURL %>% 
  read_html %>%
  html_nodes("table")

html_table(temp[16], fill = TRUE)

我认为最令人头疼的是某些列结合了图像和文本,我试图在同一列中包含来自两者的信息的数据框。例如,第 6 行的“卡名”是 Kricketune V。“Kricketune”是文本,而“V”是图片。

我觉得应该有一种简单的方法,但我似乎无法理解它。非常感谢帮助!

我发现的例子很相似: Scraping Wikipedia HTML table with images, text, and blank cells with R 但是,我不知道如何将其应用于这种情况,因为我也在尝试保留行中的文本。

【问题讨论】:

    标签: r web-scraping rvest


    【解决方案1】:

    您可以先获取表格,然后更新这些列。您可以将ifelse 用于Type 列,因为您想要的值可以在th 或子img 中存在。有趣的是使用正确的 css 选择器,以便仅匹配相关节点来更新表。

    library(rvest)
    library(tidyverse)
    
    page <- read_html("https://bulbapedia.bulbagarden.net/wiki/Battle_Styles_(TCG)#Set_lists")
    
    df <- page %>%
      html_node(".multicol .roundy tr:nth-child(2) table") %>%
      html_table(fill = T)
    
    df <- subset(df, No. != "") %>% select(-c(Image))
    
    df$Rarity <- page %>%
      html_nodes("td:nth-child(1) tr table td:nth-child(5) a") %>%
      html_attr("title")
       
    df$Type <- map(page %>% html_nodes("td:nth-child(1) tr tr:nth-child(n+2) th:nth-child(4)"), function(x) {
      var_node <- x %>%
        html_node("img") %>%
        html_attr("alt")
      ifelse(is.na(var_node), x %>% html_text(trim = T), var_node)
    })
    

    【讨论】:

    • 非常感谢 QHarr,它几乎回答了它!真的很感激。对于如何将 ifelse 用于 type 列,我仍然有点困惑,但会有更多的调查。
    • ifelse 是因为 type 并不总是包含在子 img 的 alt 属性中。在少数情况下,它只是在 td 元素本身中。如果 x %>% html_node("img") %>% html_attr("alt") 不存在,则返回 na,然后您可以使用 ifelse 处理它以返回 td %>% html_text跨度>
    猜你喜欢
    • 2019-07-10
    • 2021-07-25
    • 1970-01-01
    • 2014-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-08
    • 1970-01-01
    相关资源
    最近更新 更多