首页 > 解决方案 > 使用 rvest 抓取不在表中的数据

问题描述

我正在尝试从网站上抓取一些数据。我以为我可以使用 rvest,但我在获取不在表格中的数据时遇到了很多麻烦。

我不知道这是否可能,或者我是否使用了错误的包?

我正在尝试从以下 html 获取网站、名称和地址:

<div class="info clearfix">
<i class="sprite icon title"></i>
<p class="title">
<a target="_blank" href="https://test.com/regions/Tennis_Court.html">
Tennis Court</a>
</p>
<p class="location"> 123 Page St, Charlestown</p>                                                <p class="excerpt" itemprop="description">A place to play tennis</p>                                                                                           </div>

我希望我可以使用诸如 html_node("title") 之类的东西,但这似乎并没有错。我完全走错了路吗?

标签: rrvest

解决方案


您可以使用html_nodes添加 css 选择器来提取:

library(rvest)
url <- 'https://concreteplayground.com/auckland/bars'

webpage <- url %>% read_html()
name <- webpage %>% html_nodes('p.name a') %>%html_text() %>% trimws()
address <- webpage %>% html_nodes('p.address') %>% html_text() %>% trimws()
links <- webpage %>% html_nodes('p.name a') %>% html_attr('href')
data.frame(name, address, links)

#                              name                                address
#1                         Holy Hop          498 New North Road, Kingsland
#2                              Sly          354A Karangahape Road, Newton
#...
#...

                                                                      
#                                                                 links
#1                         https://concreteplayground.com/auckland/bars/holy-hop
#2                              https://concreteplayground.com/auckland/bars/sly
#...
#...

推荐阅读