我正在尝试抓取亚马逊产品页面,更具体地说是review部分。
我尝试过不同的代码,其中之一是:
if(!"pacman" %in% installed.packages()[,"Package"]) install.packages("pacman")
pacman::p_load(rvest, dplyr, tidyr, stringr)
# Airpods
asin_code <- "B09JQMJHXY"
url <- paste0("https://www.amazon.com/dp/", asin_code)
doc <- read_html(url)
#obtain the text in the node, remove "\n" from the text, and remove white space
prod <- html_nodes(doc, "#productTitle") %>%
html_text() %>%
gsub("\n", "", .) %>%
trimws()
prod
这只刮掉了标题,但如果我解决了这个问题,我想其余的都会好起来的。
所以问题是这样的事实:当我第一次尝试时,我得到了正确的输出(即标题)。
15 分钟后运行相同的代码,我得到
character(0)
。
这可能是什么原因造成的?
亚马逊是否屏蔽了我的 IP,或类似的东西?
R版本4.3.2
library(rvest)
page <- "https://www.amazon.com/dp/B09JQMJHXY" |>
read_html()
page |>
html_elements(".a-section.review.cr-desktop-review-page-0") |>
map_dfr(~ tibble(
title = html_element(.x, ".cr-original-review-content") |>
html_text2(),
rating = html_element(.x, ".a-icon-alt") |>
html_text2(),
review = html_element(.x, ".reviewText") |>
html_text2(),
date = html_element(.x, ".review-date") |>
html_text2()
))
# A tibble: 5 × 4
title rating review date
<chr> <chr> <chr> <chr>
1 AirPod Pro 5.0 o… Excel… Revi…
2 10/10 llegó como tenía que llegar 5.0 o… Muy b… Revi…
3 Buenos audifonos. 5.0 o… Muy b… Revi…
4 Son buenos, pero me salieron con un error, pero Apple con … 4.0 o… Este … Revi…
5 Cómodos y súper fáciles de usar 5.0 o… Son l… Revi…