您的位置:首页 > 理论基础 > 计算机网络

R语言rvest包爬取当当网单个商品的商品id,品牌,价格等商品属性

2016-10-02 14:59 543 查看
直接上代码吧!
本文章的例子仅用于学习之用,如涉及版权隐私信息,请联系本人删除,谢谢


# install.packages("rvest")
library(rvest)
url<-"http://product.dangdang.com/1328241371.html#ddclick?act=click&pos=1328241371_36_1_m&cat=4001075&key=&qinfo=&pinfo=&minfo=4438_2_48&ninfo=&custid=&permid=&ref=&rcount=&type=&t=1475383245000&searchapi_version=test_new"
# 解析商品单页
web<-read_html(url,encoding = "gbk")
# 获取商品参数内容
content<-web%>%
html_nodes("div.main")%>%
html_nodes("div.mall_goods_foursort_style")%>%
html_nodes("div")%>%
html_text()
# 商品品牌
brand<-substr(content[2],4,nchar(content[3]))
# 商品id
id<-substr(content[3],4,nchar(content[3]))
# 商品价格
price<-web%>%
html_nodes("div.sale")%>%
html_nodes("div")%>%
html_nodes("div")%>%
html_nodes("b.d_price")%>%
html_nodes("span")%>%
html_text()
item<-data.frame(brand=brand,id=id,price=price)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息