4

I want to use R to scrape the links contained within a wikimedia category tree and the structure of the tree from here. The code below can open up all the collapsible bullet points

library(RSelenium)

rD <- rsDriver(check = FALSE)
remDr <- rD[["client"]]
remDr$navigate("https://commons.wikimedia.org/wiki/Category:Sports")

n <- 1
# n <- 10 # takes a long time to expand all bullet points
for(i in 1:n){
  b <- remDr$findElements(using = "css selector", "[title='expand']")
  for(i in 1:length(b)){
    b[[i]]$clickElement()
  }
}

... but i am struggling to build a data base that would look like...

enter image description here

I can get the bullet href and names using the code below, but i am struggling to find a way to indicate which level each bullet points refers too (i.e. how deep in the category tree each bullet point is)? I am thinking there might be a clever xpath method to count how many CategoryTreeChildren deep each bullet is but that is reaching well beyond my capabilities.

# for testing I manually expand the bullets for the first couple of branches 
# (fully for Bulgaria women badminton, basketball) and the last possible 
# branch rather than let the for loop run and run through multiple cycles.
library(tidyverse)
library(rvest)
s <- remDr$getPageSource() 
d <- read_html(s[[1]]) %>%
  html_nodes("div#mw-subcategories") %>%
  html_nodes("div.CategoryTreeItem") %>%
  html_nodes("a") %>%
  map(xml_attrs) %>%
  map_df(~as.list(.)) %>%
  as_tibble()
# > d 
# # A tibble: 135 x 2
#    href                                     title                              
#    <chr>                                    <chr>                              
#  1 /wiki/Category:Categories_by_sport       Category:Categories by sport       
#  2 /wiki/Category:Categories_by_sport_by_c~ Category:Categories by sport by co~
#  3 /wiki/Category:Categories_of_Bulgaria_b~ Category:Categories of Bulgaria by~
#  4 /wiki/Category:Female_sportspeople_from~ Category:Female sportspeople from ~
#  5 /wiki/Category:Female_badminton_players~ Category:Female badminton players ~
#  6 /wiki/Category:Maria_Delcheva            Category:Maria Delcheva            
#  7 /wiki/Category:Petya_Nedelcheva          Category:Petya Nedelcheva          
#  8 /wiki/Category:Gabriela_Stoeva           Category:Gabriela Stoeva           
#  9 /wiki/Category:Stefani_Stoeva            Category:Stefani Stoeva            
# 10 /wiki/Category:Women%27s_basketball_pla~ Category:Women's basketball player~

I have also played around with the WikipediR package - it says in the package description that it can be used to retrieve elements of category trees but i cannot find an example of how to implement it.

guyabel
  • 8,014
  • 6
  • 57
  • 86

0 Answers0