使用R从TripAdvisor搜索数据

IVR*_*IVR 9 xpath r rselenium

我想创建一个可以从Trip Advisor中抓取一些数据的爬虫.理想情况下,它将 (a)识别要爬行的所有地点的链接, (b)收集每个地点所有景点的链接, (c)收集所有评论的目的地名称,日期和评级.我现在想集中讨论(a)部分.

这是我开始的网站:http: //www.tripadvisor.co.nz/Tourism-g255104-New_Zealand-Vacations.html

这里有问题:该链接提供了前10个目的地,如果您再点击"查看更多热门目的地",它将展开列表.它似乎使用javascript函数来实现这一点.不幸的是,我不熟悉javascript,但我认为下面的块可能会提供有关它如何工作的线索:

<div class="morePopularCities" onclick="ta.call('ta.servlet.Tourism.showNextChildPage', event, this)">
<img id='lazyload_2067453571_25' height='27' width='27' src='http://e2.tacdn.com/img2/x.gif'/>
See more popular destinations in New Zealand </div>
Run Code Online (Sandbox Code Playgroud)

我已经为R找到了一些有用的网页编写软件包,比如rvest,RSelenium,XML,RCurl,但是其中只有RSelenium似乎能够解决这个问题,尽管如此,我仍然无法使用它出.

这是一些相关的代码:

tu = "http://www.tripadvisor.co.nz/Tourism-g255104-New_Zealand-Vacations.html"
RSelenium::startServer()
remDr = RSelenium::remoteDriver(browserName = "internet explorer")
remDr$open()
remDr$navigate(tu)
# remDr$executeScript("JS_FUNCTION")
Run Code Online (Sandbox Code Playgroud)

最后一行应该在这里做,但我不确定我需要在这里调用什么函数.

一旦我设法扩展这个列表,我将能够以与解决(b)部分相同的方式获取每个目的地的链接,我想我已经解决了这个问题(对于那些感兴趣的人):

library(rvest)
tu = "http://www.tripadvisor.co.nz/Tourism-g255104-New_Zealand-Vacations.html"
tu = html_session(tu)
tu %>% html_nodes(xpath='//div[@class="popularCities"]/a') %>% html_attr("href")
 [1] "/Tourism-g255122-Queenstown_Otago_Region_South_Island-Vacations.html"                      
 [2] "/Tourism-g255106-Auckland_North_Island-Vacations.html"                                     
 [3] "/Tourism-g255117-Blenheim_Marlborough_Region_South_Island-Vacations.html"                  
 [4] "/Tourism-g255111-Rotorua_Rotorua_District_Bay_of_Plenty_Region_North_Island-Vacations.html"
 [5] "/Tourism-g255678-Nelson_Nelson_Tasman_Region_South_Island-Vacations.html"                  
 [6] "/Tourism-g255113-Taupo_Taupo_District_Waikato_Region_North_Island-Vacations.html"          
 [7] "/Tourism-g255109-Napier_Hawke_s_Bay_Region_North_Island-Vacations.html"                    
 [8] "/Tourism-g612500-Wanaka_Otago_Region_South_Island-Vacations.html"                          
 [9] "/Tourism-g255679-Russell_Bay_of_Islands_Northland_Region_North_Island-Vacations.html"      
[10] "/Tourism-g255114-Tauranga_Bay_of_Plenty_Region_North_Island-Vacations.html"  
Run Code Online (Sandbox Code Playgroud)

至于步骤(c),我发现了一些可能对此有用的有用链接: https ://github.com/hadley/rvest/blob/master/demo/tripadvisor.R http://notesofdabbler.github. IO/201408_hotelReview/scrapeTripAdvisor.html

如果您有关于如何扩展顶级目的地列表或如何以更智能的方式完成其他步骤的任何提示,请告诉我,我真的很想收到您的来信.

提前谢谢了!

har*_*r07 3

基本上,您可以尝试将单击事件发送到<div class="morePopularCities">. 像这样的东西:

remDr$navigate(tu)
div <- remDr$findElement("class", "morePopularCities")
div$clickElement()
Run Code Online (Sandbox Code Playgroud)

要展开所有位置,您可以在 while 循环中重复上述逻辑。继续单击<div>直到没有更多可用项目(直到div页面中不再有):

divs <- remDr$findElements("class", "morePopularCities")
while(length(divs )>0) {
  for(div in divs ){
    div$clickElement()
  }
  divs <- remDr$findElements("class", "morePopularCities")
}
Run Code Online (Sandbox Code Playgroud)

我不太流利R,您可能会发现我的代码示例不漂亮,请随意提出建议。