How to scrap with table class name with R?

Issue

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%E2%80%9312_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 class="wikitable plainrowheaders" style="text-align:center;font-size:100%;">

Many thanks in advance.

Solution

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

library(rvest)

url <- paste0("https://en.wikipedia.org/wiki/2011%E2%80%9312_Welsh_Premier_League")

webpage <- read_html(url)

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

Answered By – bdedu

Answer Checked By – David Goodson (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.