【问题标题】:How to web scrape a tree into an R list in form of a tree?如何以树的形式将树刮到R列表中?
【发布时间】:2023-03-10 16:52:01
【问题描述】:

我想将(波兰语)Wikipedia categories 的名称以树的形式提供到 R 列表中,同时保持树的结构。

我的想法是将所有类别放入一个列表中,其中子列表将指示该类别的子类别。问题是我不知道如何让刮伤进入最糟糕的孩子。

我现在拥有的是树的某些级别的单独列表,但它似乎不是最佳解决方案。此外,我先抓取链接,然后两次(几乎)完全相同的类别名称。

这是我目前写的代码:

library("stringi")
library("rvest")

find_links <- function(link){
  strona <- read_html(link)
  kategorie <- html_nodes(strona, ".CategoryTreeLabel")
  if(length(kategorie)==0) return(NA)
  as.list(stri_paste("http://pl.wikipedia.org", html_attr(kategorie, "href")))
}

find_titles <- function(link){
  strona <- read_html(link)
  kategorie <- html_nodes(strona, ".CategoryTreeLabel")
  if(length(kategorie)==0) return(NA)
  as.list(html_text(kategorie))
}

link <- 'http://pl.wikipedia.org/wiki/Wikipedia:Drzewo_kategorii'
strona <- read_html(link)
kategorie <- html_nodes(strona, ".CategoryTreeLabel")[-c(1:10, 14, 69, 96, 101, 155, 176, 188, 220, 234)] //don't need these categories
kategorie <- kategorie[220:225] //taking a sample so it doesn't take long
linki <- as.list(stri_paste("http://pl.wikipedia.org", html_attr(kategorie, "href")))
tytuly <- as.list(html_text(kategorie))
tytuly <- list(tytuly, linki)


aa <- rapply(linki, find_links,  how = 'replace')
aa1 <- rapply(linki, find_titles, how = 'replace')
bb <- rapply(aa, find_links,  how = 'replace')
bb1 <- rapply(aa, find_titles,  how = 'replace')
...

这是 bb1(树的第二层)的结果:

> bb1
[[1]]
[[1]][[1]]
[1] NA

[[1]][[2]]
[[1]][[2]][[1]]
[1] "Odznaczenia Związku Ochotniczych Straży Pożarnych Rzeczypospolitej Polskiej"


[[1]][[3]]
[1] NA

[[1]][[4]]
[[1]][[4]][[1]]
[1] "Pożary według państw"

请帮忙。我愿意接受任何其他保持类别结构的解决方案。

【问题讨论】:

    标签: r tree web-scraping rvest


    【解决方案1】:

    类似这样的:

    library(stringi)
    library(rvest)
    library(dplyr)
    
    expand_tree = function(link, level) {
      print(link)
    
      tree_nodes = 
        link %>%
        read_html %>%
        html_nodes(".CategoryTreeLabel")
    
      if (length(tree_nodes) > 0)
        data_frame(link = 
                     tree_nodes %>% 
                     html_attr("href") %>%
                     paste0("http://pl.wikipedia.org", .),
                   name = html_text(tree_nodes) ) %>%
        setNames(names(.) %>% paste(level, . , sep = ".") ) else data_frame() }
    
    level1 = 
      'http://pl.wikipedia.org/wiki/Wikipedia:Drzewo_kategorii' %>%
      expand_tree("level1") %>%
      slice(-c(1:10, 14, 69, 96, 101, 155, 176, 188, 220, 234)) %>%
      slice(220:225)
    
    level2 = 
      level1 %>%
      group_by(level1.link) %>%
      do(expand_tree(first(.$level1.link), "level2")) %>%
      ungroup
    
    level3 = 
      level2 %>%
      group_by(level2.link) %>%
      do(expand_tree(first(.$level2.link), "level3")) %>%
      ungroup
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-04
      • 1970-01-01
      • 2015-06-14
      • 1970-01-01
      • 1970-01-01
      • 2021-12-05
      • 2011-11-18
      • 1970-01-01
      相关资源
      最近更新 更多