r - 无法使用来自 r 的 rvest 包读取带有 read_html 的网页
问题描述
我正在尝试从亚马逊获取产品评论者的位置。例如,这个网页
我需要得到HAINESVILLE, ILLINOIS, United States
我使用 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]
推荐阅读
- sql - 日期期间最近的员工职位
- rust - 如何使用不在 crates.io 上的库?
- moodle - Moodle API 函数 core_course_import_course 不起作用
- javascript - Reactjs - 是的:如何检查 2 个输入是否相等?
- javascript - 如何使用 keyup 功能进行多表单输入
- php - 为什么 Openssl 使用 openssl_decrypt 函数抛出错误?
- c# - 最小化 .NET 代码或提高效率
- c# - 如何使用 Vaultsharp 读取机密
- kotlin - 我可以在消费者指定回放长度的情况下构建 Kotlin SharedFlow 吗?
- find - 临时表从迭代表以查找多个级别的相关记录