Rvest选择选项,我认为这是最容易解释的一个例子重现
网站:http://www.verema.com/vinos/portada我想得到的葡萄酒的类型(Tipos de vinos),在html代码是:
<select class="campo select" id="producto_tipo_producto_id" name="producto[tipo_producto_id]">
<option value="">Todos</option>
<option value="211">Tinto</option>
<option value="213">Blanco</option>
<option value="215">Rosado</option>
<option value="216">Espumoso</option>
<option value="217">Dulces y Generosos</option></select>
XPath : //*[@id="producto_tipo_producto_id"] or
CSS : #producto_tipo_producto_id or
Class: campo select
我想要一个 Dataframe 作为
小行星211
小布兰科213
小行星215
埃斯普莫索216
217 Dulces y Generosos
我的代码(R):
library(rvest)
Pagina.R <- html(x = "http://www.verema.com/vinos/portada")
text <- Pagina.R %>%
html_nodes(xpath='//*[@id="producto_tipo_producto_id"]')%>%
html_text()
text
values <- Pagina.R %>%
html_nodes(xpath='//*[@id="producto_tipo_producto_id"]')%>%
html_attr("option value") #problem????
values
Res <- data.frame(text = text, values = values, stringsAsFactors = FALSE)
Res # problem
建议?
谢谢大家。
更新
修订后的功能代码:
library(rvest)
Pagina.R <- html(x = "http://www.verema.com/vinos/portada")
text <- Pagina.R %>%
# html_nodes(xpath='//*[@id="producto_tipo_producto_id"]')%>%
html_nodes(xpath='//*[@id="producto_tipo_producto_id"]/option')%>%
html_text()
text
values <- Pagina.R %>%
# html_nodes(xpath='//*[@id="producto_tipo_producto_id"]')%>%
html_nodes(xpath='//*[@id="producto_tipo_producto_id"]/option')%>%
# html_attr("option value")
html_attr("value")
values
Res <- data.frame(text = text, values = values, stringsAsFactors = FALSE)
Res
1条答案
按热度按时间3qpi33ja1#
以下是可以考虑的另一种方法: