使用 rvest 抓取不在 table 中的数据
Using rvest to scrape data that is not in table
我正在尝试从网站上抓取一些数据。我以为我可以使用 rvest,但是我在获取不在 table.
中的数据时遇到了很多麻烦
不知道能不能,还是我用错包了?
我正在尝试从以下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") 之类的东西,但这似乎并没有错。我完全走错路了吗?
您可以使用 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
#...
#...
我正在尝试从网站上抓取一些数据。我以为我可以使用 rvest,但是我在获取不在 table.
中的数据时遇到了很多麻烦不知道能不能,还是我用错包了?
我正在尝试从以下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") 之类的东西,但这似乎并没有错。我完全走错路了吗?
您可以使用 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
#...
#...