楼主: daishen
2126 3

[程序分享] 抓取xml网页数据 [推广有奖]

  • 0关注
  • 10粉丝

已卖:127份资源

博士生

6%

还不是VIP/贵宾

-

威望
0
论坛币
3155 个
通用积分
51.3742
学术水平
67 点
热心指数
68 点
信用等级
42 点
经验
3733 点
帖子
111
精华
0
在线时间
324 小时
注册时间
2014-9-14
最后登录
2016-3-10

楼主
daishen 发表于 2014-12-2 19:41:37 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
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




二维码

扫码加我 拉你入群

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

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

关键词:xml网页 XML strawberries Description breakfast 网页

已有 2 人评分经验 论坛币 热心指数 收起 理由
李会超 + 40 + 20 精彩帖子
LIXUANHANK + 1 精彩帖子

总评分: 经验 + 40  论坛币 + 20  热心指数 + 1   查看全部评分

沙发
suhui0723 在职认证  发表于 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) 是?? 求解答

藤椅
daishen 发表于 2014-12-2 20:30:55
html_nodes(id) 应该是  html_nodes(id[i])
网页显示出问题了。
%>%是下水管道

板凳
pohys 在职认证  发表于 2014-12-31 23:27:38
用正则匹配吧,应该可以行得通过的

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-1-18 13:51