r - 无法使用 r 中的 rvest 包通过 read_html 读取网页

标签 r web-scraping rvest

我正在尝试从亚马逊获取产品评论者的位置。例如这个网页

[https://www.amazon.com/gp/profile/amzn1.account.AH55KF4JK5IKKJ77MPOLHOR4YAQQ/ref=cm_cr_dp_d_gw_tr?ie=UTF8][1]

我需要美国伊利诺伊州HAINESVILLE

我使用 rvest 包进行网页抓取。

这是我所做的:

library(rvest)       
url='https://www.amazon.com/gp/profile/amzn1.account.AH55KF4JK5IKKJ77MPOLHOR4YAQQ/ref=cm_cr_dp_d_gw_tr?ie=UTF8'
page = read_html(url)

我收到如下错误:

Error in open.connection(x, "rb") : HTTP error 403.

但是,以下方法有效:

con <- url(url, "rb")
page = read_html(con)

但是,在我阅读的页面中,我无法提取任何文本。比如我想提取审稿人的位置。

page %>%
    html_nodes("#customer-profile-name-header .a-size-base a-color-base")%>%
    html_text()

我什么都没有

character(0)

谁能帮我弄清楚我做错了什么吗?预先非常感谢。

最佳答案

这应该有效:

library(dplyr)
library(rvest)
library(stringr)

# get url
url='https://www.amazon.com/gp/profile/amzn1.account.AH55KF4JK5IKKJ77MPOLHOR4YAQQ/ref=cm_cr_dp_d_gw_tr?ie=UTF8'

# open page
con <- url(url, "rb")
page = read_html(con)

# get the desired information, using View Page Source
page %>%
  html_nodes(xpath=".//script[contains(., 'occupation')]")%>%
  html_text() %>% as.character() %>% str_match(.,"location\":\"(.*?)\",\"personalDescription") -> res

res[,2]

关于r - 无法使用 r 中的 rvest 包通过 read_html 读取网页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56064293/

相关文章:

r - 使用 R 抓取您自己的 Stack Overflow 配置文件

r - 如何构建新的中心性度量?

rbind 两个表并用具有相同变量的值填充 NA 的行

R:使用lubridate计算年龄

r - 使用 XML 和 Rvest 在 R 中进行网页抓取

r - 使用 R 和 rvest 进行网页抓取

r - 从网站提取 .csv 文件

R用聚类绘制热图,但隐藏树状图

python - 在 Python 中填写动态在线表单

google-apps-script - Google Apps 抓取脚本以正常运行,直到提取所有网站的内页?