R - 使用 rvest 抓取 Google + 评论
R - Using rvest to scrape Google + reviews
作为项目的一部分,我试图从 Google 中抓取完整的评论 +(在之前在其他网站上的尝试中,我的评论被 More
截断,这隐藏了完整的评论除非你点击它)。
我为此选择了包 rvest。但是,我似乎没有得到我想要的结果。
这是我的步骤
library(rvest)
library(xml2)
library(RSelenium)
queens <- read_html("https://www.google.co.uk/search?q=queen%27s+hospital+romford&oq=queen%27s+hospitql+&aqs=chrome.1.69i57j0l5.5843j0j4&sourceid=chrome&ie=UTF-8#lrd=0x47d8a4ce4aaaba81:0xf1185c71ae14d00,1,,,")
#Here I use the selectorgadget tool to identify the user review part that I wish to scrape
reviews=queens %>%
html_nodes(".review-snippet") %>%
html_text()
但这似乎不起作用。我在这里没有得到任何输出。
我对这个包和网络抓取还很陌生,所以任何关于这方面的意见都将不胜感激。
这是使用 RSelenium 和 rvest 的工作流程:
1.随时向下滚动以获取尽可能多的内容,记得暂停一段时间让内容加载。
2. 单击所有 "click on more" 按钮并获得完整评论。
3. 获取 pagesource 并使用 rvest 获取列表中的所有 reveiws
你要抓取的不是静态的,所以你需要RSelenium的帮助。这应该有效:
library(rvest)
library(xml2)
library(RSelenium)
rmDr=rsDriver(browser=c("chrome"), chromever="73.0.3683.68")
myclient= rmDr$client
myclient$navigate("https://www.google.co.uk/search?q=queen%27s+hospital+romford&oq=queen%27s+hospitql+&aqs=chrome.1.69i57j0l5.5843j0j4&sourceid=chrome&ie=UTF-8#lrd=0x47d8a4ce4aaaba81:0xf1185c71ae14d00,1,,,")
#click on the snippet to switch focus----------
webEle <- myclient$findElement(using = "css",value = ".review-snippet")
webEle$clickElement()
#simulate scroll down for several times-------------
scroll_down_times=20
for(i in 1 :scroll_down_times){
webEle$sendKeysToActiveElement(sendKeys = list(key="page_down"))
#the content needs time to load,wait 1 second every 5 scroll downs
if(i%%5==0){
Sys.sleep(1)
}
}
#loop and simulate clicking on all "click on more" elements-------------
webEles <- myclient$findElements(using = "css",value = ".review-more-link")
for(webEle in webEles){
tryCatch(webEle$clickElement(),error=function(e){print(e)}) # trycatch to prevent any error from stopping the loop
}
pagesource= myclient$getPageSource()[[1]]
#this should get you the full review, including translation and original text-------------
reviews=read_html(pagesource) %>%
html_nodes(".review-full-text") %>%
html_text()
#number of stars
stars <- read_html(pagesource) %>%
html_node(".review-dialog-list") %>%
html_nodes("g-review-stars > span") %>%
html_attr("aria-label")
#time posted
post_time <- read_html(pagesource) %>%
html_node(".review-dialog-list") %>%
html_nodes(".dehysf") %>%
html_text()
作为项目的一部分,我试图从 Google 中抓取完整的评论 +(在之前在其他网站上的尝试中,我的评论被 More
截断,这隐藏了完整的评论除非你点击它)。
我为此选择了包 rvest。但是,我似乎没有得到我想要的结果。
这是我的步骤
library(rvest)
library(xml2)
library(RSelenium)
queens <- read_html("https://www.google.co.uk/search?q=queen%27s+hospital+romford&oq=queen%27s+hospitql+&aqs=chrome.1.69i57j0l5.5843j0j4&sourceid=chrome&ie=UTF-8#lrd=0x47d8a4ce4aaaba81:0xf1185c71ae14d00,1,,,")
#Here I use the selectorgadget tool to identify the user review part that I wish to scrape
reviews=queens %>%
html_nodes(".review-snippet") %>%
html_text()
但这似乎不起作用。我在这里没有得到任何输出。
我对这个包和网络抓取还很陌生,所以任何关于这方面的意见都将不胜感激。
这是使用 RSelenium 和 rvest 的工作流程:
1.随时向下滚动以获取尽可能多的内容,记得暂停一段时间让内容加载。
2. 单击所有 "click on more" 按钮并获得完整评论。
3. 获取 pagesource 并使用 rvest 获取列表中的所有 reveiws
你要抓取的不是静态的,所以你需要RSelenium的帮助。这应该有效:
library(rvest)
library(xml2)
library(RSelenium)
rmDr=rsDriver(browser=c("chrome"), chromever="73.0.3683.68")
myclient= rmDr$client
myclient$navigate("https://www.google.co.uk/search?q=queen%27s+hospital+romford&oq=queen%27s+hospitql+&aqs=chrome.1.69i57j0l5.5843j0j4&sourceid=chrome&ie=UTF-8#lrd=0x47d8a4ce4aaaba81:0xf1185c71ae14d00,1,,,")
#click on the snippet to switch focus----------
webEle <- myclient$findElement(using = "css",value = ".review-snippet")
webEle$clickElement()
#simulate scroll down for several times-------------
scroll_down_times=20
for(i in 1 :scroll_down_times){
webEle$sendKeysToActiveElement(sendKeys = list(key="page_down"))
#the content needs time to load,wait 1 second every 5 scroll downs
if(i%%5==0){
Sys.sleep(1)
}
}
#loop and simulate clicking on all "click on more" elements-------------
webEles <- myclient$findElements(using = "css",value = ".review-more-link")
for(webEle in webEles){
tryCatch(webEle$clickElement(),error=function(e){print(e)}) # trycatch to prevent any error from stopping the loop
}
pagesource= myclient$getPageSource()[[1]]
#this should get you the full review, including translation and original text-------------
reviews=read_html(pagesource) %>%
html_nodes(".review-full-text") %>%
html_text()
#number of stars
stars <- read_html(pagesource) %>%
html_node(".review-dialog-list") %>%
html_nodes("g-review-stars > span") %>%
html_attr("aria-label")
#time posted
post_time <- read_html(pagesource) %>%
html_node(".review-dialog-list") %>%
html_nodes(".dehysf") %>%
html_text()