从 Trip Advisor 中提取星级
Extract Star Rating from Trip Advisor
为了我的自学目的(网络爬虫),我想从 trip advisor 网站上提取用户给出的评分。下面是我用 R 编写的代码,但它不起作用。你能给我建议或给我正确的代码吗?我也没有成功使用 Rvest 包。
library(XML)
dat <- readLines("http://www.tripadvisor.in/Hotel_Review-g60763-d93450-Reviews-Grand_Hyatt_New_York-New_York_City_New_York.html", warn=FALSE)
raw2 <- htmlTreeParse(dat, useInternalNodes = TRUE)
##Rating
data <- xpathApply(raw2,"//div[@class='col2of2']//span[@class='rate sprite-rating_s rating_s']/img[@alt]",xmlValue)
Rating <- sapply(data,function(x) xmlAttrs(x)["alt"])
Result <- unlist(Rating)
Result
您可以使用
##Rating
Result <- xpathSApply(raw2,"//div[@class='col2of2']//span[@class='rate sprite-rating_s rating_s']/img", xmlGetAttr, "alt")
为了我的自学目的(网络爬虫),我想从 trip advisor 网站上提取用户给出的评分。下面是我用 R 编写的代码,但它不起作用。你能给我建议或给我正确的代码吗?我也没有成功使用 Rvest 包。
library(XML)
dat <- readLines("http://www.tripadvisor.in/Hotel_Review-g60763-d93450-Reviews-Grand_Hyatt_New_York-New_York_City_New_York.html", warn=FALSE)
raw2 <- htmlTreeParse(dat, useInternalNodes = TRUE)
##Rating
data <- xpathApply(raw2,"//div[@class='col2of2']//span[@class='rate sprite-rating_s rating_s']/img[@alt]",xmlValue)
Rating <- sapply(data,function(x) xmlAttrs(x)["alt"])
Result <- unlist(Rating)
Result
您可以使用
##Rating
Result <- xpathSApply(raw2,"//div[@class='col2of2']//span[@class='rate sprite-rating_s rating_s']/img", xmlGetAttr, "alt")