RVest:从超市网页中提取 class 内的标签内容

RVest: extract tag content inside a class from a supermarket webpage

我正在尝试使用 Rvest 抓取 一个网站,但我被卡住了,找不到解决方案。 我设法在 R 中成功加载页面并提取了我想要的 HTML 部分,但我无法获得我想要的数据。

我要的HTML片(不是真的URL)是:

<a class="open-modal" data-id="53357" href="http://www.website.com.ar/product_1.html">
<img id="prod-image-53357" src="https://www.website.com.ar/media/98989898.jpg"
                           alt="insecticide 360 cc" />
</a>

我的代码:

library("rvest")
library("xml2")

url <- "https://www.website.com.ar/limpieza.html"
page <- read_html(url)
d <-page %>% html_nodes(" .open-modal")

如果我检查对象 d,例如 d[[4]],我得到:

{html_node}
<a class="open-modal" data-id="53357" href="https://www.website.com.ar/product_1.html">
[1] <img id="prod-image-53357" src="https://www.website.com.ar/media/98989898.jpg
alt="insecticide 360 cc" />

我想将 href、alt 和 src 提取为文本并将它们转换为数据框...

我试过:

d <-page %>% html_nodes(" .open-modal") %>% html_text()

d <-page %>% html_nodes(" .open-modal") %>% html_text('href')

没有成功...

有什么帮助吗? 提前致谢!

或许,你应该使用 html_attr :

library(rvest)
d <- page %>% html_nodes("a.open-modal") 
data.frame(href = d %>% html_attr('href'), 
           alt = d %>% html_attr('alt'),
           src = d %>% html_attr('src'))