我正在从一个特定的
类中抓取网站上的所有文本。在以下示例中,我想提取所有在 class="a" 的
中的内容。
``` 下面的代码提取了每个
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"。``` 下面的代码提取了每个
元素中的文本,但是我无法想出如何仅包含 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")
。