Home > Enterprise >  How to scrap with table class name with R?
How to scrap with table class name with R?

Time:05-26

I am tryng to scrap several web pages, particulaty some tables in the pages. But the problem is the places of tables change with respect to each page. Here is my code.

url <- paste0("https://en.wikipedia.org/wiki/2011–12_Welsh_Premier_League")
webpage <- read_html(url)
j<-webpage%>% html_node(xpath='//*[@id="mw-content-text"]/div[1]/table') %>%html_table(fill=T)

This code works fine, but I want to scrap the other seaons, too. The place of table changes in every season. My question is I found that the table class that I want to scrap is named as "wikitable plainrowheaders", as below. I would like to know how to scrap with table class name. How to scrap all tables with table class named as "wikitable plainrowheaders" in a wikipedia page?

<table  style="text-align:center;font-size:100%;">

Many thanks in advance.

CodePudding user response:

Since you know the table class name, just change the corresponding xpath.

library(rvest)

url <- paste0("https://en.wikipedia.org/wiki/2011–12_Welsh_Premier_League")

webpage <- read_html(url)

j <- webpage %>%
  html_nodes(xpath="//table[@class='wikitable plainrowheaders']") %>%
  html_table(fill=T) 

  • Related