R_Language 板


LINE

※ 引述《celestialgod (天)》之铭言: : ※ 引述《jojojen (JJJ)》之铭言: : : [问题类型]: : : 程式谘询 : : [软体熟悉度]: : : 入门(写过其他程式,只是对语法不熟悉) : : [问题叙述]: : : 各位大大好,小弟算是爬虫初学者,最近在练习爬取联合新闻的即时新闻列表, : : 在抓出版时间时碰到一点问题,虽然硬是写了出来, : : 但还是想请教一下有没有更好的写法 : : 麻烦各位了!! : : [程式范例]: : : # install pack : : list.of.packages <- c("rvest", "RCurl", "stringi", "XML", "stringr") : : new.packages <- list.of.packages[!(list.of.packages %in% : : installed.packages()[,"Package"])] : : if(length(new.packages)) install.packages(new.packages) : : # 捞取财经新闻 : : surl = "http://money.udn.com/money/breaknews" : : udn = read_html(surl,encoding="UTF-8") : : ranking_table = udn %>% html_nodes('.area_body') %>% html_nodes(xpath = : : "//table") : : title = ranking_table %>% html_nodes('a') %>% html_text %>% iconv(from = : : 'UTF-8', to = 'UTF-8') : : url = ranking_table %>% html_nodes('a') %>% html_attr('href') : : ## 抓取时间的时候,因为类别跟出版时间都被放在only_web class里 : : ## 我分不开只好都先抓下来,再砍掉不符合的栏位 : : pattern = '^[0-9]{2}' : : t = ranking_table %>% html_nodes('.only_web') %>% html_text %>% as.data.frame : : colnames(t) = c("data") : : time = subset(t, grepl(pattern, t$data)) : : [环境叙述]: : : R version 3.3.1 (2016-06-21) : : Platform: x86_64-w64-mingw32/x64 (64-bit) : : Running under: Windows 7 x64 (build 7600) : : [关键字]: : : 网路爬虫, RVEST : 下面是我的作法,windows用rvest会遇到encoding问题 : 但是windows中文版也不能正常显示UTF8字元,所以要经过一点转换 : # require pkgs and install it if it is not installed : if (!"installr" %in% installed.packages()) install.packages("installr") : library(installr) : require2(rvest) : require2(stringi) : require2(data.table) # 转不转data.table无所谓 : require2(pipeR) : if (is.windows()) { : original_locale <- Sys.getlocale("LC_COLLATE") : Sys.setlocale("LC_ALL", 'C') : } : surl <- "http://money.udn.com/money/breaknews" : outTbl <- read_html(surl, encoding="UTF-8") %>>% : html_node("#ranking_table") %>>% html_table : if (is.windows()) { : outTblTrans <- lapply(outTbl, function(v){ : if (class(v) == "character") { : return(stri_conv(v, to = "big5")) # 字串都转成big5 : } else { : return(v) : } : }) %>>% `names<-`(NULL) %>>% as.data.table %>>% # names一定要先清空不然会错 : setnames(stri_conv(names(outTbl), to = "big5")) : Sys.setlocale(locale = original_locale) : } : # 标题 类别 出版时间 浏览数 分享数 : # 1: 共享单车成都投放1周 乱停放被城管没收 即时 11/25 21:21 0 NA : # 2: 缅甸洛兴雅遭迫害 翁山苏姬成众矢之的 即时 11/25 21:21 0 NA : # 3: 马英九:我还没上任 就被批评会带来大灾难 即时 11/25 21:02 11 NA : # 4: 控缅甸种族净化洛兴雅人 亚洲爆示威 即时 11/25 20:54 8 NA : 上面那段也可以换成下面这个做法,不过是data.table only (但是资料量大会快一点) : if (is.windows()) { : outTblTrans <- outTbl[ , lapply(.SD, stri_conv, to = "big5")] %>>% : setnames(stri_conv(names(outTbl), to = "big5")) : Encoding(names(outTblTrans)) <- "big5" : Sys.setlocale(locale = original_locale) : } 以下是根据celestialgod大的教学完成的爬虫,万分感谢celestialgod大的协助!!! # require pkgs and install it if it is not installed if (!"installr" %in% installed.packages()) install.packages("installr") library(installr) require2(rvest) # to get web content require2(stringr) # to collapse a list of characters into a single string require2(pipeR) # 载入爬取新闻本文func getUdnNewsCont <- function(url) { udn = read_html(url,encoding="UTF-8") artic = udn %>% html_nodes('p') %>% html_text() %>% iconv(from = 'UTF-8', to = 'UTF-8') %>% str_c(collapse='.') return(artic) } # 捞取财经新闻 pb_news <- txtProgressBar(1, 60, style=3) # 进度条 ## get news list if (is.windows()) { original_locale <- Sys.getlocale("LC_COLLATE") Sys.setlocale("LC_ALL", 'C') } surl <- "http://money.udn.com/money/breaknews" outTbl <- read_html(surl, encoding="UTF-8") %>>% html_node("#ranking_table") %>>% html_table ranking_table = read_html(surl, encoding="UTF-8") %>>% html_node("#ranking_table") if (is.windows()) { outTblTrans <- lapply(outTbl, function(v){ if (class(v) == "character") { return(stri_conv(v, to = "big5")) # 字串都转成big5 } else { return(v) } }) %>>% `names<-`(NULL) %>>% as.data.table %>>% # names一定要先清空不然会错 setnames(stri_conv(names(outTbl), to = "big5")) Sys.setlocale(locale = original_locale) } ## get news content domain = "http://money.udn.com/" url = ranking_table %>% html_nodes('a') %>% html_attr('href') %>% paste0(domain, .) content <- character(60) for (i in c(1:length(url))) { content[i] <- getUdnNewsCont(url[i]) setTxtProgressBar(pb_news, i) } news <- data.frame(outTbl, url=url, content=content) news <- data.frame(lapply(news, as.character), stringsAsFactors=FALSE) ## clean table View(news) --



※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 175.98.68.200
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/R_Language/M.1480086515.A.57C.html
1F:→ lovedmagic: 在跑code的时候html_table的部分跑出乱码 12/03 16:42
2F:→ lovedmagic: > %>>% html_table 12/03 16:44
3F:→ lovedmagic: é什麽的乱码这边无法复制,抱歉 12/03 16:45
4F:→ celestialgod: 请给系统 12/03 17:00
5F:推 celestialgod: windows都要跑Sys.setlocale("LC_ALL", 'C') 12/03 17:01
6F:→ celestialgod: 会建议跑我回文那篇的程式 12/03 17:02
7F:推 lovedmagic: 好的非常感谢 12/03 17:14







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灯, 水草

请输入看板名称,例如:WOW站内搜寻

TOP