3

I'm attempting to do something very similar to this question.

However, I need a slightly different table. Sounds easy enough, right? But I've run into a weird case of "same code appears to work differently in two instances." Here's a simple test example.

If I run the below code, the first instance runs fine. The second returns the following error:

Error in UseMethod("html_table") : no applicable method for 'html_table' >applied to an object of class "xml_missing"

The table names seem consistent with what I'm seeing in SelectorGadget.

library(rvest)
url_splits    <-"http://www.basketball-reference.com/players/i/ingrabr01/splits/"
stats_splits  <- read_html(url_splits) %>%
             html_node("table#splits") %>%
             html_table()
url_college   <-"http://www.basketball-reference.com/players/i/ingrabr01.html"
stats_college <- read_html(url_college) %>%
                 html_node("table#all_college_stats") %>%
             html_table()

Obviously something must be different but I can't tell what it is. I tried using the xpath fro Selector instead with the same result. Any ideas?

Community
  • 1
  • 1
CRS1834
  • 111
  • 1
  • 6

1 Answers1

3

So, I have discovered the answer. Apparently the second table is nested inside of a comment a la this question.

In case anyone stumbles across this, the fixed code is below:

stats_college <- url %>%
             read_html %>%
             html_nodes(xpath = '//comment()') %>%
             html_text() %>%
             paste(collapse='') %>%
             read_html() %>%
             html_node('table#all_college_stats') %>%
             html_table()
Community
  • 1
  • 1
CRS1834
  • 111
  • 1
  • 6