I want to use the packages rvest
to pull gas prices from a web page. However, I can not pull the numeric values and have to pull by the html class .sp_p
.
library(rvest)
desmoines <- html("http://www.desmoinesgasprices.com/")
Pull gas prices:
price <- desmoines %>%
html_nodes(".sp_p")
head(price, 3)
Output:
[[1]]
<div class="sp_p">
<div class="p2"></div>
<div class="pd"></div>
<div class="p5"></div>
<div class="p5"></div>
</div>
[[2]]
<div class="sp_p">
<div class="p2"></div>
<div class="pd"></div>
<div class="p5"></div>
<div class="p6"></div>
</div>
[[3]]
<div class="sp_p">
<div class="p2"></div>
<div class="pd"></div>
<div class="p5"></div>
<div class="p7"></div>
</div>
attr(,"class")
[1] "XMLNodeSet"
Now, I want to use the package stringr
to extract the digits from the web scrape, but I can't use stringr
because price
is not an atomic vector. How do I get around this?
Here's one possibility: