【发布时间】:2015-10-21 04:14:06
【问题描述】:
我想解析这个 HTML:并从中获取这些元素:
a) p 标签,带有class: "normal_encontrado"。
b) div 和 class: "price"。
有时,某些产品中不存在p 标签。如果是这种情况,则应将NA 添加到从该节点收集文本的向量中。
这个想法是有 2 个长度相同的向量,然后将它们连接起来形成一个data.frame。有什么想法吗?
HTML部分:
<html>
<head></head>
<body>
<div class="product_price" id="product_price_186251">
<p class="normal_encontrado">
S/. 2,799.00
</p>
<div id="WC_CatalogEntryDBThumbnailDisplayJSPF_10461_div_10" class="price">
S/. 2,299.00
</div>
</div>
<div class="product_price" id="product_price_232046">
<div id="WC_CatalogEntryDBThumbnailDisplayJSPF_10461_div_10" class="price">
S/. 4,999.00
</div>
</div>
</body>
</html>
R 代码:
library(rvest)
page_source <- read_html("r.html")
r.precio.antes <- page_source %>%
html_nodes(".normal_encontrado") %>%
html_text()
r.precio.actual <- page_source %>%
html_nodes(".price") %>%
html_text()
【问题讨论】:
-
这样的事情可能会有所帮助 - R dataframe from xml when values are multiple or missing
标签: r tags web-scraping rvest