全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 R语言论坛
2089 3
2014-12-02
library(rvest)

url = "http://www.w3schools.com/xml/simple.xml"

或者把网页下载到本地

url = "c:/temp/w31.xml"
id = c("name","price","calories","description")
dat = sapply(1:length(id),function(i) url %>% html() %>% html_nodes(id) %>% html_text()) %>% as.data.frame(.)
names(dat) = id

dat
                                    name price   calories
1                    Belgian Waffles $5.95      650
2  Strawberry Belgian Waffles $7.95      900
3 Berry-Berry Belgian Waffles $8.95      900
4                      French Toast $4.50      600
5           Homestyle Breakfast $6.95      950
                                                                                                                              description
1                             \r\nTwo of our famous Belgian Waffles with plenty of real maple syrup\r\n
2                            \r\nLight Belgian waffles covered with strawberries and whipped cream\r\n
3 \r\nLight Belgian waffles covered with an assortment of fresh berries and whipped cream\r\n
4                                           \r\nThick slices made from our homemade sourdough bread\r\n
5                     \r\nTwo eggs, bacon or sausage, toast, and our ever-popular hash browns\r\n

看起来有点乱,修饰一下:
dat$description = gsub("\r|\n",'',dat$description)
dat
                                    name price      calories                                                                                                            description
1                    Belgian Waffles $5.95     650                             Two of our famous Belgian Waffles with plenty of real maple syrup
2  Strawberry Belgian Waffles $7.95     900                            Light Belgian waffles covered with strawberries and whipped cream
3 Berry-Berry Belgian Waffles $8.95     900 Light Belgian waffles covered with an assortment of fresh berries and whipped cream
4                      French Toast $4.50     600                                           Thick slices made from our homemade sourdough bread
5           Homestyle Breakfast $6.95     950                     Two eggs, bacon or sausage, toast, and our ever-popular hash browns




二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2014-12-2 20:02:07
dat = sapply(1:length(id),function(i) url %>% html() %>% html_nodes(id) %>% html_text()) %>% as.data.frame(.)
names(dat) = id


这一句里面%>%是什么意思?后面这一大串是函数参数吗,  sapply里的function(i) 是?? 求解答
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2014-12-2 20:30:55
html_nodes(id) 应该是  html_nodes(id[i])
网页显示出问题了。
%>%是下水管道
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2014-12-31 23:27:38
用正则匹配吧,应该可以行得通过的
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群