从网页的HTML代码中提取特定信息?

时间:2016-05-03 06:47:08

标签: r html-parsing dplyr rcurl stringr

我有一个网页“http://www.jabong.com/playdate-Off-White-Casual-Top-1342500.html?pos=1”,我可以获取它的HTML代码......但我需要提取特定信息......从上面的页面我需要以下信息:< / p>

类型:休闲上衣,面料:棉,袖子:半袖,颈部:圆领,适合:普通,洗涤护理:手洗,使用温和洗涤剂,去除腰带/拉刀前洗,颜色:白色,面料细节:95/5棉莱卡,款式:平面,SKU:PL527KA99JYQINDFAS

1 个答案:

答案 0 :(得分:2)

您需要一个HTML抓取器/解析器,例如rvest

library(rvest)

url <- 'http://www.jabong.com/playdate-Off-White-Casual-Top-1342500.html?pos=1'

# get HTML, select list node with the information
page <- url %>% read_html() %>% html_node('.prod-main-wrapper') 

# select the nodes within the list of each type, and get the text inside
variable <- page %>% html_nodes('label') %>% html_text()
value <- page %>% html_nodes('span') %>% html_text()

# put the text in a nice data.frame
data.frame(variable, value)
#          variable                                                               value
# 1            Type                                                         Casual Tops
# 2          Fabric                                                              Cotton
# 3         Sleeves                                                        Half Sleeves
# 4            Neck                                                          Round neck
# 5             Fit                                                             Regular
# 6       Wash Care Hand Wash, Use Mild Detergents, Remove Belts / Broaches Before Wash
# 7           Color                                                           Off White
# 8  Fabric Details                                                   95/5 Cotton Lycra
# 9           Style                                                             Graphic
# 10            SKU                                                  PL527KA99JYQINDFAS
# 11  Authorization          Playdate authorized online sales partner. View Certificate