楼主: daishen
1995 1

[R] 用R来抓取xml网页数据 [推广有奖]

  • 0关注
  • 10粉丝

已卖:127份资源

博士生

6%

还不是VIP/贵宾

-

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

楼主
daishen 发表于 2015-3-4 12:27:42 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

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

经管之家联合CDA

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

感谢您参与论坛问题回答

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

+2 论坛币
      用R来抓取xml网页数据。


  1. library(rvest)

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

  3. 或者把网页下载到本地

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

  8. dat
  9.                                     name price   calories
  10. 1                    Belgian Waffles $5.95      650
  11. 2  Strawberry Belgian Waffles $7.95      900
  12. 3 Berry-Berry Belgian Waffles $8.95      900
  13. 4                      French Toast $4.50      600
  14. 5           Homestyle Breakfast $6.95      950
  15.                                                                                                                               description
  16. 1                             \r\nTwo of our famous Belgian Waffles with plenty of real maple syrup\r\n
  17. 2                            \r\nLight Belgian waffles covered with strawberries and whipped cream\r\n
  18. 3 \r\nLight Belgian waffles covered with an assortment of fresh berries and whipped cream\r\n
  19. 4                                           \r\nThick slices made from our homemade sourdough bread\r\n
  20. 5                     \r\nTwo eggs, bacon or sausage, toast, and our ever-popular hash browns\r\n
复制代码


      看起来有点乱,修饰一下:

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




二维码

扫码加我 拉你入群

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

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

关键词:xml网页 XML strawberries Description breakfast xml网页 数据抓取

已有 3 人评分经验 论坛币 学术水平 热心指数 信用等级 收起 理由
Nicolle + 20 + 20 精彩帖子
niuniuyiwan + 100 + 100 + 3 + 5 + 5 精彩帖子
李会超 + 40 精彩帖子

总评分: 经验 + 160  论坛币 + 120  学术水平 + 3  热心指数 + 5  信用等级 + 5   查看全部评分

本帖被以下文库推荐

沙发
laodong1983 发表于 2015-3-4 12:36:56
提示: 作者被禁止或删除 内容自动屏蔽

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

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2026-1-25 03:56