使用 rvest 从网页中提取名称列表和底层超链接
Extracting list of names and underlying hyperlinks from webpage using rvest
我是网络抓取的新手,正在尝试掌握使用 rvest
从网页收集数据的方法。感兴趣的网页是 https://www.cabq.gov/office-of-neighborhood-coordination/neighborhood-homeowner-coalition-websites,它提供了社区组织的列表以及指向这些组织网站的基础超链接。我正在尝试生成一个数据框,其中第一列是组织名称,第二列是超链接中的 URL。
我已经按照几个 rvest
教程和 Stack Overflow 问题尝试解析出适当的节点以提取我感兴趣的信息,但无济于事。所需的输出看起来像这样(...
只是在输出 table 目标的所需开头和结尾之间截断中间的输出):
| organization | URL |
| ---------------------------------------------- | ----------------------------------- |
| 7 Bar North Homeowners Association | https://www.7barnorthhoa.com/ |
| Academy Acres North Neighborhood Association | http://www.aanna.org/ |
....
| Willow Wood Neighborhood Association | http://www.hoamcoweb.com/willowwood |
| Winrock Villas Condominium Association | http://winrockvillas.hoaspace.com/ |
我的代码尝试如下。
library(xml2)
library(rvest)
library(tidyverse)
URL <- "https://www.cabq.gov/office-of-neighborhood-coordination/neighborhood-homeowner-coalition-websites"
pg <- read_html(URL)
html_nodes(pg, "external-link") %>%
map_df(function(x) {
data_frame(
postal = html_node(x, "span") %>% html_text(trim=TRUE),
city = html_nodes(x, "ul > li") %>% html_text(trim=TRUE)
)
})
#> # A tibble: 0 x 0
由 reprex package (v0.3.0)
于 2021-02-15 创建
非常感谢任何帮助。
首先,我认为您需要使用 xpath 表达式来获取 link 的正确类型。您对 class external-link 的 a 个元素感兴趣,因此您可以使用:
html_nodes(pg, xpath="//a[@class='external-link']")
您可以构建更复杂的 xpath 表达式来满足您的需要。然后需要提取元素的文本和一个属性,可以使用:
html_nodes(pg, xpath="//a[@data-linktype='external' or
@class='external-link']") %>%
map_df(function(x) {
data_frame(
organization = x %>% html_text(trim=TRUE),
URL = x %>% html_attr("href")
)})
我是网络抓取的新手,正在尝试掌握使用 rvest
从网页收集数据的方法。感兴趣的网页是 https://www.cabq.gov/office-of-neighborhood-coordination/neighborhood-homeowner-coalition-websites,它提供了社区组织的列表以及指向这些组织网站的基础超链接。我正在尝试生成一个数据框,其中第一列是组织名称,第二列是超链接中的 URL。
我已经按照几个 rvest
教程和 Stack Overflow 问题尝试解析出适当的节点以提取我感兴趣的信息,但无济于事。所需的输出看起来像这样(...
只是在输出 table 目标的所需开头和结尾之间截断中间的输出):
| organization | URL |
| ---------------------------------------------- | ----------------------------------- |
| 7 Bar North Homeowners Association | https://www.7barnorthhoa.com/ |
| Academy Acres North Neighborhood Association | http://www.aanna.org/ |
....
| Willow Wood Neighborhood Association | http://www.hoamcoweb.com/willowwood |
| Winrock Villas Condominium Association | http://winrockvillas.hoaspace.com/ |
我的代码尝试如下。
library(xml2)
library(rvest)
library(tidyverse)
URL <- "https://www.cabq.gov/office-of-neighborhood-coordination/neighborhood-homeowner-coalition-websites"
pg <- read_html(URL)
html_nodes(pg, "external-link") %>%
map_df(function(x) {
data_frame(
postal = html_node(x, "span") %>% html_text(trim=TRUE),
city = html_nodes(x, "ul > li") %>% html_text(trim=TRUE)
)
})
#> # A tibble: 0 x 0
由 reprex package (v0.3.0)
于 2021-02-15 创建非常感谢任何帮助。
首先,我认为您需要使用 xpath 表达式来获取 link 的正确类型。您对 class external-link 的 a 个元素感兴趣,因此您可以使用:
html_nodes(pg, xpath="//a[@class='external-link']")
您可以构建更复杂的 xpath 表达式来满足您的需要。然后需要提取元素的文本和一个属性,可以使用:
html_nodes(pg, xpath="//a[@data-linktype='external' or
@class='external-link']") %>%
map_df(function(x) {
data_frame(
organization = x %>% html_text(trim=TRUE),
URL = x %>% html_attr("href")
)})