R_Language 板


LINE

※ 引述《corel (可罗)》之铭言: : [问题类型]: : 程式谘询(我想用R 做某件事情,但是我不知道要怎麽用R 写出来) : [软体熟悉度]: : 入门(写过其他程式,只是对语法不熟悉) : [问题叙述]: : 请简略描述你所要做的事情,或是这个程式的目的 : # Q:想要撷取出网页中某区块标签所有的文字,包含html标签等内的某一资料 : #结果: : #撷取出网页内所有同属性的资料... : # crawl library ---- : library(bitops) : library(XML) : library(RCurl) : # only for windows ----- : signatures <- system.file("CurlSSL", cainfo="cacert.pem", package="RCurl") : # 网址缩址 http://tinyurl.com/jgoun9c : home <- : "http://www.expodatabase.com/aussteller/messen/index.php?OK=1&sortierid=0&maxPerPage=20&i_cockpitkeyfindwo=2&i_cockpitkeyfindart=1&currPage=1" : home <- getURL(home, cainfo = signatures) : home <- htmlParse(home) : # 共有20笔的block : block <-getNodeSet(home, "//div[@class='shm']") : # length(block) # 共20笔 : doc <- block[[1]] # 抓取第一笔 : # doc : # <div class="shm"> : # <div class="listdates"> : # <div class="date">08Jan-17Jan2016</div> : # </div> : # <div class="search_result_list_box"> : # <div class="city">London, United Kingdom</div> : # <div class="firma"><a : href="show.php?id=352&amp;timer=m1452657261&amp;tmid=&amp;currPage=1&amp;maxPerPage=20&amp;params=timer%3Dm1452657261%26amp%3Btimer%3Dm1452657261%26amp%3Bi_cockpitkeyfindwo%3D2%26amp%3Bi_cockpitkeyfindart%3D1%26amp%3Bsortierid%3D0%26amp%3Btimer%3Dm1452657261%26amp%3BmaxPerPage%3D20%26amp%3BshowPrintlist%3D0%26amp%3BmaxPerPage%3D20">London : Boat Show</a></div> : # </div> : # <div class="search_result_box_right"> : # <div class="branchen"><strong>Business sectors:</strong> Boats</div> : # </div> : # <div class="fixfloat"></div> : # </div> : # 想要抓取 block[[1]]笔的 class="date" 的 08Jan-17Jan2016 : date <- xpathSApply(doc, "//div[@class='date']", xmlValue) : # 结果秀出网页内全部 date 的资料 : # [1] "08Jan-17Jan2016" "08Jan-17Jan2016" "09Jan-17Jan2016" "07Jan-15Jan2017" : "06Jan-14Jan2018" : # [6] "09Jan-17Jan2016" "09Jan-17Jan2016" "09Jan-17Jan2016" "09Jan-17Jan2016" : "10Jan-13Jan2016" : # [11] "10Jan-13Jan2016" "10Jan-13Jan2016" "10Jan-13Jan2016" : "11Jan-13Jan2016" "11Jan-13Jan2016" : # [16] "11Jan-13Jan2016" "11Jan-14Jan2016" "11Jan-14Jan2016" : "11Jan-14Jan2016" "11Jan-14Jan2016" : # [21] "11Jan-14Jan2016" "11Jan-24Jan2016" "09Jan-22Jan2017" : 我的问题是, doc <- block[[1]] : 不是己经抓取出 於 block 中第一笔资料并且储存在doc中吗? : 结果用 xpathSpply 撷出来的..竟然是网页中 date 全部资料? : 想请教一下, 我该用那一个函数或方法才能只抓到 block[[1]]中 xml的资料? : 并且解析出 date 为 08Jan-17Jan2016 ? : 谢谢 : #线上程式: : # http://ideone.com/ZqHAqr 个人经验,XML需要比较多的记忆体又比较慢,还有可能有memory leak的问题 我推荐你用xml2这个套件XD library(xml2) library(magrittr) library(RCurl) home <- "http://www.expodatabase.com/aussteller/messen/index.php?OK=1&sortierid=0& maxPerPage=20&currPage=1" signatures <- system.file("CurlSSL", cainfo="cacert.pem", package="RCurl") html_page <- getURL(home, cainfo = signatures) read_html(html_page) %>% xml_find_all(".//div[@class='shm']") %>% .[[1]] %>% xml_find_all(".//div[@class='date']") %>% xml_text # [1] "08Jan-17Jan2016" -- R资料整理套件系列文: magrittr #1LhSWhpH (R_Language) http://tinyurl.com/1LhSWhpH data.table #1LhW7Tvj (R_Language) http://tinyurl.com/1LhW7Tvj dplyr(上) #1LhpJCfB (R_Language) http://tinyurl.com/1LhpJCfB dplyr(下) #1Lhw8b-s (R_Language) tidyr #1Liqls1R (R_Language) http://tinyurl.com/1Liqls1R --



※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 140.109.74.87
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/R_Language/M.1452676654.A.3E6.html
1F:→ Wush978: 请问有对XML批评的出处吗?纯追根究底 01/14 07:14
上面就写个人经验啦XDD 我自己试的结果 之前parse了上万个xml,发现XML真的没那麽好用QQ xml2比较方便XD stackoverflow随便一搜,其实满多人有在讲XML有memory leak的问题..
2F:推 corel: PUSH 01/15 07:45
※ 编辑: celestialgod (140.109.74.87), 01/15/2016 11:41:35







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:Boy-Girl站内搜寻

TOP