3

I try to using rvest package to scrape a table:

library(rvest)

x <- read_html ("http://www.jcb.jp/rate/usd04182016.html")
x %>% html_node(".CSVTable") %>% html_table 

Url elements look likes:

<table class="CSVTable">
 <tbody>...</tbody>
 <tbody class>...</tbody>
</table>

Why I occur the error "No matches"?

1
  • Take a look at ?html_table - "Parse an html table...".Then try x %>% html_table. You see that the table you're seeing on the page can't be parsed by html_table Commented Apr 19, 2016 at 3:51

1 Answer 1

4

You're in luck (kind of). The site uses dynamic XHR requests to make that table, but said request is also a CSV file.

library(rvest)
library(stringr)

pg <- read_html("http://www.jcb.jp/rate/usd04182016.html")

# the <script> tag that does the dynamic loading is in position 6 of the 
# list of <script> tags

fil <- str_match(html_text(html_nodes(pg, "script")[6]), "(/uploads/[[:digit:]]+\\.csv)")[,2]

df <- read.csv(sprintf("http://www.jcb.jp%s", fil), header=FALSE, stringsAsFactors=FALSE)

df <- setNames(df[,3:6], c("buy", "mid", "sell", "symbol"))

head(df)
##        buy      mid     sell symbol
## 1   3.6735   3.6736   3.6737    AED
## 2  68.2700  69.0700  69.8700    AFN
## 3 122.3300 122.6300 122.9300    ALL
## 4 479.5000 481.0000 482.5000    AMD
## 5   1.7710   1.8110   1.8510    ANG
## 6 165.0600 165.3100 165.5600    AOA

But, that also means you can just get the CSV directly:

read.csv("http://www.jcb.jp/uploads/20160418.csv")

(just format the date properly in your requests).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.