【发布时间】:2022-09-24 11:19:42
【问题描述】:
我正在尝试从该网页获取有关啤酒类型和最受欢迎的位置的数据:https://untappd.com/La_Source
我写了代码:
library(rvest)
library(dplyr)
link = \"https://untappd.com/La_Source\"
page = read_html(link)
name = page %>% html_nodes(\".user\") %>% html_text()
place = page %>% html_nodes(\"a:nth-child(4)\") %>% html_text()
user = page %>% html_nodes(\".user\") %>% html_text()
user_links = page %>% html_nodes(\".user\") %>%
html_attr(\"href\") %>% paste(\"https://untappd.com/\", ., sep=\"\")
get_city = function(user_link) {
# user_link= \'https://untappd.com/user/Linty\'
user_page = read_html(user_link)
user_city = user_page %>% html_nodes(\".location\") %>%
html_text() %>% paste(collapse = \",\")
return(user_city)
}
city = sapply(user_links, FUN = get_city, USE.NAMES = FALSE)
#brewery = page %>% html_nodes(\"a:nth-child(3)\") %>% html_text()
Beer = data.frame(name, place,user,city, stringsAsFactors = FALSE)
write.csv(Beer, \"Beer.csv\")
效果非常好,并为我提供了所需的数据。当我尝试通过在页面底部按加载更多按钮 \'\' 来获取更多数据时出现问题。我不确定如何在 R 中做到这一点。有什么建议吗?
-
请改用RSelenium 之类的东西。这使您可以像使用网络浏览器一样与网页进行交互,并且您可以编写代码来为您按下按钮。另一种选择是使用您的浏览器开发工具来尝试查看数据的来源,看看您是否可以直接获取数据。
标签: r web-scraping load