【发布时间】:2017-03-15 19:43:23
【问题描述】:
我正在努力寻找简洁的代码来执行以下操作:
- 查找包含五个项目的列表
- 遍历所有五个项目
- 从每个项目中提取 4 列
- 返回一个包含五行的数据框,每行一个。
示例 HTML:
<div class="i-am-a-list">
<div class="item item-one"><a href=""></a><a class="title"></a><p>sub-title</p></div>
<div class="item item-two"><a href=""></a><a class="title-two"></a><p>sub-title</p></div>
<div class="item item-three"><a href=""></a><a class="title-three"></a><p>sub-title</p></div>
<div class="item item-four"><a href=""></a><a class="title-for"></a><p>sub-title</p></div>
<div class="item item-five"><a href=""></a><a class="title-five"></a><p>sub-title</p></div>
</div>
到目前为止的代码:
# find the upper list
coll <- read_html(doc.html) %>%
html_node('.i-am-a-list') %>%
html_nodes(".item")
# problems here, how do I iterate over the returned divs
# I was expecting something like
results <- coll %>%
do(parse_a_single_item) %>%
rbind_all()
是否有可能编写如此漂亮的代码来完成如此常见的任务? :)
【问题讨论】: