gpt4 book ai didi

r - 使用R从TripAdvisor抓取数据

转载 作者:行者123 更新时间:2023-12-03 15:24:32 25 4
gpt4 key购买 nike

我想创建一个爬网程序,该爬网程序将从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>

我发现了一些针对R的有用的webscraping软件包,例如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")

最后一行应该可以解决问题,但是我不确定我需要在这里调用什么功能。

一旦我设法扩展了此列表,我将能够以与解决(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"

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

如果您有关于如何扩展主要目的地列表或如何以更明智的方式完成其他步骤的任何提示,请告诉我,我非常希望收到您的来信。

提前谢谢了!

最佳答案

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

remDr$navigate(tu)
div <- remDr$findElement("class", "morePopularCities")
div$clickElement()

要扩展所有位置,您可以在while循环中重复上述逻辑。继续单击 <div>,直到没有更多可用项为止(直到页面中不再存在 div为止):
divs <- remDr$findElements("class", "morePopularCities")
while(length(divs )>0) {
for(div in divs ){
div$clickElement()
}
divs <- remDr$findElements("class", "morePopularCities")
}

我不太会使用 R,您可能会发现我的代码示例不够漂亮,请随时提出建议。

关于r - 使用R从TripAdvisor抓取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29713443/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com