【发布时间】:2018-06-30 14:03:32
【问题描述】:
我正在从网站上抓取特定类 div 中出现的所有文本。在下面的示例中,我想提取“a”类 div 中的所有内容。
site <- "<div class='a'>Hello, world</div>
<div class='b'>Good morning, world</div>
<div class='a'>Good afternoon, world</div>"
我想要的输出是...
"Hello, world"
"Good afternoon, world"
下面的代码从每个 div 中提取文本,但我不知道如何只包含 class="a"。
library(tidyverse)
library(rvest)
site %>%
read_html() %>%
html_nodes("div") %>%
html_text()
# [1] "Hello, world" "Good morning, world" "Good afternoon, world"
使用 Python 的 BeautifulSoup,它看起来像 site.find_all("div", class_="a")。
【问题讨论】: