Dan L
Dan L

Reputation: 67

Scraping text with rvest

I am trying to scrape a text item from the following website: http://www.fangraphs.com/statss.aspx?playerid=639&position=3B
The item I want to scrape is the "3B" from that follows "Position:" near the top of the web page. My attempt below only creates a empty dataset aka:
character(0)

Any help is appreciated. Thanks.

library(rvest)
url="http://www.fangraphs.com/statss.aspx?playerid=10155&position=3B"       
ret <- url %>%
    read_html() %>%
    html_nodes(xpath='//*[@id="content"]/table[1]/tbody/tr/td[1]/table/tbody/tr[1]/td/div[2]/text()[4]') %>%
    html_text()

ret

Upvotes: 1

Views: 377

Answers (1)

GGamba
GGamba

Reputation: 13680

Use this xpath: //*[@id="content"]/table[1]/tr/td[1]/table/tr[1]/td/div[2]/text()[5] It is basically the same as yours, except that it excludes tbody tags -not sure why, can't find it documented- and changes the last index to 5.

Result for playerid=639: " 3B"

(add %>% trimws() at the end to get "3B")

Upvotes: 1

Related Questions