Re: [问题] 爬虫爬取联合财经新闻

楼主: jojojen (JJJ)   2016-11-25 23:08:33
※ 引述《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)
作者: lovedmagic (EricZou)   2016-12-03 16:42:00
在跑code的时候html_table的部分跑出乱码> %>>% html_tableé什么的乱码这边无法复制,抱歉
作者: celestialgod (天)   2016-12-03 17:00:00
请给系统windows都要跑Sys.setlocale("LC_ALL", 'C')会建议跑我回文那篇的程式
作者: lovedmagic (EricZou)   2016-12-03 17:14:00
好的非常感谢

Links booklink

Contact Us: admin [ a t ] ucptt.com