r - 使用 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") 之类的东西,但这似乎并没有错。我完全走错了路吗?
解决方案
您可以使用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
#...
#...
推荐阅读
- r - 在 R markdown 中报告而不显示不必要的字符,例如 [ [1] ]
- php - 即使文件具有 777 权限,LibreOffice 转换为 pdf 也会产生文件无法加载错误
- python - 是什么导致我在 tensorflow 中出现分段错误?
- python - 如何用不同的行和列替换较大矩阵中的小矩阵
- python - 使用 OpenCV 和 Numpy 从另一个图像中提取的像素坐标创建多通道图像
- exception - SML - 在快速排序算法中找不到“未捕获异常空”的位置
- javascript - 同名函数但基于条件执行
- python - 在矩形列表中移动单个矩形pygame
- python - 如何根据数据框的长度从 groupby 对象创建文件
- python - Instagram API - 如何获取我在我的帐户上关注的人的简历?