问题描述
我正在尝试抓取Google搜索的标题。但是,不管我使用rvest
做什么,结果总是返回character(0)
。
library(rvest)
library(dplyr)
web1 <- read_html("https://www.google.at/search?q=rstudio")
header <-web1 %>%
html_nodes(".DKV0Md") %>%
html_text()
header
我在SelectorGadget
中签入的节点名称,因此这不是问题。我该如何解决这个问题?
解决方法
也许我们可以使用:
library(rvest)
library(dplyr)
web1 %>%
html_nodes(xpath = '//div/div/div/a/div[not(div)]') %>%
html_text
输出:
#[1] "rstudio.com"
#[2] "rstudio.cloud"
#[3] "en.wikipedia.org › wiki › RStudio"
# ....