r - Using "rvest" scraping html table -
i try using rvest package scrape table:
library(rvest) x <- read_html ("http://www.jcb.jp/rate/usd04182016.html") x %>% html_node(".csvtable") %>% html_table
url elements likes:
<table class="csvtable"> <tbody>...</tbody> <tbody class>...</tbody> </table>
why occur error "no matches"?
you're in luck (kind of). site uses dynamic xhr requests make table, said request csv file.
library(rvest) library(stringr) pg <- read_html("http://www.jcb.jp/rate/usd04182016.html") # <script> tag dynamic loading in position 6 of # 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 ## 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, means can csv directly:
read.csv("http://www.jcb.jp/uploads/20160418.csv")
(just format date in requests).
Comments
Post a Comment