r - 将html_nodes转换为R中的数据框
问题描述
我正在从网页中提取数据
url <- "https://www.lacolonia.com/vinagre-tinto-carbonell-250-ml-ref8410010855064/p"
我想要一个 data_frame 中的所有数据
html <- read_html(url) #leyendo HTML
productos <- html_nodes(html,".product-details , .skuBestPrice") #extrayendo el HTML en bruto
links5 <- html_nodes(productos, "a")
links6 <- bind_rows(lapply(xml_attrs(links5), function(x) data.frame(as.list(x), stringsAsFactors=FALSE)))
链接没有来自“productos”的一些数据,缺少价格值
productos 为我提供此信息
[1] <div class="row product-details "><div class="">\n<div class="col-xs-12 hidden-sm description-prod"><div class="c ...
[2] <strong productindex="0" class="skuBestPrice">L. 75.90</strong>
我需要 data_frame 中的 value= 75.90,你能帮我写代码吗
解决方案
您可以使用 :
library(rvest)
url <- "https://www.lacolonia.com/vinagre-tinto-carbonell-250-ml-ref8410010855064/p"
url %>%
read_html() %>%
html_nodes('p.descricao-preco strong.skuBestPrice') %>%
html_text() %>%
sub('.*?(\\d+\\.\\d+).*', '\\1', .) %>% as.numeric()
#For optional decimal numbers
#sub('.*?(\\d+\\.?\\d?).*', '\\1', .) %>% as.numeric()
#[1] 75.9
推荐阅读
- python - 使用索引号同时更改 pandas 数据框中的多个列名(不是所有列名)
- android - 使用 Mircosoft 图形 REST API 进行自动发现
- html - href vs 链接反应?JS 不是基于 HTML 标签加载的
- php - 从一个表中获取数组数据并插入到另一个表中
- python - 如何从嵌套元组列表中生成批量数据?
- javascript - Javascript用0替换最后一个数字而不是用指数符号表示数字
- sql - 基于某些内容的总和表
- java - 通过 jni 从 Java 调用时,C++ iostreams 不起作用
- c# - 通过在redis缓存中传递一个键来获取多个值
- r - 有没有办法创建一个带有 n 种颜色的 ggplot 多面散点图,以便颜色交替或从调色板中随机化?