【发布时间】:2023-01-03 17:38:03
【问题描述】:
我想从页面https://www.gonser.ch/ 中抓取平均产品评级。 我看到评级显示为 5 颗可能的星星中有多少被填充的百分比宽度(金色)。 使用我的代码,我可以以某种方式提取一些宽度,但不完全是我想要的值:
page <- read_html("http://www.gonser.ch/13879")
# extract the div element
div_element <- html_nodes(page, ".feedback-stars-overlay-wrap")
# Extract the "style" attribute from the element
style_attribute <- html_attr(div_element, "style")
# extract the width value
width_value <- str_extract(style_attribute, "width: ([0-9.]+)%")
# Convert to a numeric value
width <- as.numeric(width_value)
结果,我得到 style_attribute 的 9 个不同的值,其中 2 个是 NA,其他的不是我检查页面时看到的值(在这个例子中,此时它是 width: 91.6%邮政)
有谁知道如何正确提取平均星级的宽度? 非常感谢您!
【问题讨论】:
标签: r web-scraping rvest