FINFO 爬蟲
# 1. httr => library(httr)
# 2. rvest => library(rvest)
library(rvest)
## Warning: package 'rvest' was built under R version 3.4.2
## Loading required package: xml2
url <- 'https://finfo.tw/prod_cates/%E6%AE%98%E5%BB%A2%E9%9A%AA'
# as.character(read_html(url))
# read_html(url) %>% as.character()
products <- read_html(url) %>% html_nodes('.product-line')
#products[1] %>% as.character()
name <- products %>%
html_nodes('.name') %>%
html_text()
contract <- products %>%
html_nodes('.contract') %>%
html_text()
cate <- products %>%
html_nodes('.cate') %>%
html_text()
term <- products %>%
html_nodes('.term') %>%
html_text()
buy <- products %>%
html_nodes('.buy') %>%
html_text()
finfo <- data.frame(name = name, contract = contract, cate = cate, term = term, buy = buy)
getIssuance <- function(product_cate){
url <- paste0('https://finfo.tw/prod_cates/', product_cate)
products <- read_html(url) %>% html_nodes('.product-line')
name <- products %>%
html_nodes('.name') %>%
html_text()
contract <- products %>%
html_nodes('.contract') %>%
html_text()
cate <- products %>%
html_nodes('.cate') %>%
html_text()
term <- products %>%
html_nodes('.term') %>%
html_text()
buy <- products %>%
html_nodes('.buy') %>%
html_text()
data.frame(name = name, contract = contract, cate = cate, term = term, buy = buy)
}
issuances <- c('%E5%A3%BD%E9%9A%AA',
'%E6%AE%98%E5%BB%A2%E9%9A%AA',
'%E9%87%8D%E5%A4%A7%E7%96%BE%E7%97%85%E9%9A%AA',
'%E7%99%8C%E7%97%87%E9%9A%AA',
'%E9%86%AB%E7%99%82%E9%9A%AA',
'%E6%84%8F%E5%A4%96%E9%9A%AA')
issuances_list <- lapply(issuances,getIssuance)
issuances_df <- do.call(rbind, issuances_list)
head(issuances_df)
## name contract cate term buy
## 1 友邦人壽平安定期壽險 主約 壽險 定期 752 人
## 2 遠雄人壽雄安心終身保險(106) 主約 壽險 終身 480 人
## 3 (停售)遠雄人壽雄安心終身保險 主約 壽險 終身 201 人
## 4 全球人壽終身壽險 主約 壽險 終身 130 人
## 5 友邦人壽平準終身壽險 主約 壽險 終身 45 人
## 6 遠雄人壽千禧一年期定期壽險 主約 壽險 一年期 32 人
nrow(issuances_df)
## [1] 117
issuances_df[grep('富邦人壽', issuances_df$name), ]
## name contract cate
## 10 富邦人壽新平準終身壽險 主約 壽險
## 13 富邦人壽好享福終身壽險 主約 壽險
## 16 富邦人壽優樂定期壽險(非吸菸體) 主約 壽險
## 25 富邦人壽優樂定期壽險(吸菸體) 主約 壽險
## 46 富邦人壽安富久久殘廢照護終身壽險 主約 殘扶險
## 47 (停售)富邦人壽安康久久殘廢照護終身壽險 主約 殘扶險
## 75 (停售)富邦人壽新綜合住院醫療保險附約 附約 實支實付
## 79 富邦人壽長馨健康保險附約 附約 實支實付
## 82 富邦人壽新住院醫療定期健康保險附約 附約 實支實付
## 98 富邦人壽安心寶意外傷害保險附約(死殘) 附約 意外死殘
## 99 富邦人壽日額型意外傷害住院醫療保險附約 附約 意外日額
## 100 富邦人壽安心寶意外傷害保險附約(一般實支) 批註 / 附加條款 意外實支
## 114 富邦人壽安心寶意外傷害保險附約(健保實支) 批註 / 附加條款 意外實支
## term buy
## 10 終身 9 人
## 13 終身 5 人
## 16 定期 1 人
## 25 定期 0 人
## 46 終身 0 人
## 47 終身 0 人
## 75 一年期 9 人
## 79 一年期 0 人
## 82 一年期 0 人
## 98 一年期 11 人
## 99 一年期 9 人
## 100 一年期 8 人
## 114 一年期 0 人
levels(issuances_df$cate)
## [1] "壽險" "殘扶險" "殘廢險" "重大疾病" "重大傷病"
## [6] "特定傷病" "癌症險" "癌症醫療" "實支實付" "重大燒燙傷"
## [11] "產險意外" "意外日額" "意外死殘" "意外實支"
清理資料
library(rvest)
getArticle <- function(url){
e <- read_html(url)
article = e %>% html_nodes('.ndArticle_margin p') %>%
html_text() %>% paste(., collapse='')
title <- e %>% html_nodes('h1') %>% html_text()
dt <- e %>% html_nodes('.ndArticle_creat') %>%
html_text()
len <- e %>% html_nodes('.ndArticle_view') %>% length()
clicked <- 0
if (len > 0) {
clicked <- e %>% html_nodes('.ndArticle_view') %>%
html_text()
}
category <- e %>% html_nodes('.ndgTag .current') %>%
html_text() %>% .[1]
data.frame(article = article, title = title, dt=dt, category=category, clicked = clicked, stringsAsFactors = FALSE )
}
dfall <- data.frame()
getURL <- function(newsurl){
apple <- read_html(newsurl)
rtddt <- apple %>% html_nodes('.rtddt a')
for(ele in rtddt){
url <- ele %>% html_attr('href')
#print(url)
df <- getArticle(url)
dfall <- rbind(dfall, df)
}
dfall
}
applenews <- getURL('https://tw.appledaily.com/new/realtime')
str(applenews)
## 'data.frame': 30 obs. of 5 variables:
## $ article : chr "中視電視電影《宅急變》由郎祖筠、郎祖明共同製作,朱陸豪、單承矩、林嘉俐、吳政迪等主演, 是一部驚悚推理劇。林嘉俐"| __truncated__ "台鐵東部春節車票昨開賣,全天約賣出60萬張車票,其中熱門時段普悠瑪號、太魯閣號和自強號等往返花蓮、台東長途票售光"| __truncated__ "(新增動新聞)利菁人不在東京,卻也成了大雪受災戶?!她在臉書轉貼日本新聞,東京品川區一棟大樓的屋簷掉下來,砸傷2"| __truncated__ "義大利米蘭近郊,當地時間周四(25日)上午,發生一起火車出軌意外,目前已知造成2人死亡,意外發生在距離米蘭約40公里的"| __truncated__ ...
## $ title : chr "製作人捲500萬落跑 林嘉俐無奈「整組人好幸運」" "台鐵東部熱門票一票難求 賀陳旦提三大解方" "利菁東京大樓出事!屋簷掉落砸傷2童" "米蘭近郊火車出軌 至少2死" ...
## $ dt : chr "出版時間:2018/01/25 15:55" "出版時間:2018/01/25 15:55" "出版時間:2018/01/25 15:54" "出版時間:2018/01/25 15:54" ...
## $ category: chr "娛樂" "生活" "娛樂" "國際" ...
## $ clicked : chr "0" "0" "37175" "0" ...
class(applenews$clicked)
## [1] "character"
applenews$clicked <- as.integer(applenews$clicked)
strsplit(applenews$dt,':')
## [[1]]
## [1] "出版時間" "2018/01/25 15:55"
##
## [[2]]
## [1] "出版時間" "2018/01/25 15:55"
##
## [[3]]
## [1] "出版時間" "2018/01/25 15:54"
##
## [[4]]
## [1] "出版時間" "2018/01/25 15:54"
##
## [[5]]
## [1] "出版時間" "2018/01/25 15:52"
##
## [[6]]
## [1] "出版時間" "2018/01/25 15:50"
##
## [[7]]
## [1] "出版時間" "2018/01/25 15:49"
##
## [[8]]
## [1] "出版時間" "2018/01/25 15:48"
##
## [[9]]
## [1] "出版時間" "2018/01/25 15:47"
##
## [[10]]
## [1] "出版時間" "2018/01/25 15:45"
##
## [[11]]
## [1] "出版時間" "2018/01/25 15:42"
##
## [[12]]
## [1] "出版時間" "2018/01/25 15:42"
##
## [[13]]
## [1] "出版時間" "2018/01/25 15:41"
##
## [[14]]
## [1] "出版時間" "2018/01/25 15:41"
##
## [[15]]
## [1] "出版時間" "2018/01/25 15:40"
##
## [[16]]
## [1] "出版時間" "2018/01/25 15:39"
##
## [[17]]
## [1] "出版時間" "2018/01/25 15:39"
##
## [[18]]
## [1] "出版時間" "2018/01/25 15:38"
##
## [[19]]
## [1] "出版時間" "2018/01/25 15:37"
##
## [[20]]
## [1] "出版時間" "2018/01/25 15:36"
##
## [[21]]
## [1] "出版時間" "2018/01/25 15:36"
##
## [[22]]
## [1] "出版時間" "2018/01/25 15:36"
##
## [[23]]
## [1] "出版時間" "2018/01/25 15:35"
##
## [[24]]
## [1] "出版時間" "2018/01/25 15:35"
##
## [[25]]
## [1] "出版時間" "2018/01/25 15:33"
##
## [[26]]
## [1] "出版時間" "2018/01/25 15:32"
##
## [[27]]
## [1] "出版時間" "2018/01/25 15:31"
##
## [[28]]
## [1] "出版時間" "2018/01/25 15:30"
##
## [[29]]
## [1] "出版時間" "2018/01/25 15:30"
##
## [[30]]
## [1] "出版時間" "2018/01/25 15:30"
a <- c('aaa@bbb@ccc')
strsplit(a,'@')
## [[1]]
## [1] "aaa" "bbb" "ccc"
a <- c('aaa@bbb@ccc', 'ddd@eee', 'fff')
strsplit(a,'@')
## [[1]]
## [1] "aaa" "bbb" "ccc"
##
## [[2]]
## [1] "ddd" "eee"
##
## [[3]]
## [1] "fff"
s <- ' this is a sample test '
trimws(s)
## [1] "this is a sample test"
trimws(s, 'l')
## [1] "this is a sample test "
?trimws
## starting httpd help server ... done
trimws(s, 'r')
## [1] " this is a sample test"
paste('hello','world')
## [1] "hello world"
paste0('hello','world')
## [1] "helloworld"
a <- 'aaa@bbb@ccc'
strsplit(a, '@') %>%
.[[1]] %>% paste(collapse='')
## [1] "aaabbbccc"
#head(applenews)
ds <- c("3 18, 2014 12:00")
x <- strptime(ds, "%m %d, %Y %H:%M")
?strptime
x
## [1] "2014-03-18 12:00:00 CST"
x <- strptime(ds, "%m %d, %Y %H:%M")
x1 <- as.POSIXlt(as.Date('2014-08-15'))
x - x1
## Time difference of -149.8333 days
#head(applenews)
applenews$dt <- lapply(applenews$dt, function(e){
res <- strsplit(e,"出版時間:") %>%
.[[1]] %>%
.[2]
})
applenews$dt <- unlist(applenews$dt)
applenews$dt <- as.POSIXct(applenews$dt)
a <- '出版時間:2018-01-25 11:15:00'
?gsub
sub('出版時間:', '', a)
## [1] "2018-01-25 11:15:00"
保險資料清理
head(issuances_df)
## name contract cate term buy
## 1 友邦人壽平安定期壽險 主約 壽險 定期 752 人
## 2 遠雄人壽雄安心終身保險(106) 主約 壽險 終身 480 人
## 3 (停售)遠雄人壽雄安心終身保險 主約 壽險 終身 201 人
## 4 全球人壽終身壽險 主約 壽險 終身 130 人
## 5 友邦人壽平準終身壽險 主約 壽險 終身 45 人
## 6 遠雄人壽千禧一年期定期壽險 主約 壽險 一年期 32 人
str(issuances_df)
## 'data.frame': 117 obs. of 5 variables:
## $ name : Factor w/ 117 levels "(停售)遠雄人壽美滿致富2增額終身壽險",..: 6 23 2 9 7 21 20 8 22 16 ...
## $ contract: Factor w/ 4 levels "主約","批註 / 附加條款",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ cate : Factor w/ 14 levels "壽險","殘扶險",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ term : Factor w/ 3 levels "一年期","定期",..: 2 3 3 3 3 1 1 2 3 3 ...
## $ buy : Factor w/ 52 levels "0 人","1 人",..: 14 11 6 4 10 8 7 5 3 16 ...
issuances_df$name <- as.character(issuances_df$name)
issuances_df$buy <- as.character(issuances_df$buy)
# method 1
issuances_df$buy <- as.integer(trimws(sub('人', '', issuances_df$buy)))
# method 2
issuances_df$buy <- issuances_df$buy %>%
sub('人', '', . ) %>%
trimws() %>%
as.integer()
head(issuances_df[order(issuances_df$buy, decreasing = TRUE),], 3)
## name contract cate term buy
## 26 友邦人壽友備無患一年定期保險附約 附約 殘扶險 一年期 1232
## 27 友邦人壽十一助行殘廢照顧保險附約 附約 殘廢險 一年期 1231
## 71 全球人壽醫療費用健康保險附約 附約 實支實付 一年期 1106
i835
row <- read_html('http://forum.i835.com.tw/forum-f27/') %>%
html_nodes('.forumbg .row')
titles <- row %>% html_nodes('.topictitle') %>% html_text()
urls <- row %>% html_nodes('.topictitle') %>% html_attr('href')
span <- row %>% html_nodes('span') %>% html_text()
time <- unlist(lapply(strsplit(span, '\t'), function(e) e[6]))
data.frame(time = time, titles = titles, urls = urls, stringsAsFactors = FALSE)
## time
## 1 2017-04-24, 11:34
## 2 2016-10-11, 16:36
## 3 2015-08-20, 14:58
## 4 2015-06-17, 17:16
## 5 2016-04-27, 09:17
## 6 2018-01-25, 14:48
## 7 2018-01-24, 22:40
## 8 2018-01-24, 22:32
## 9 2018-01-24, 17:09
## 10 2018-01-24, 16:58
## 11 2018-01-24, 16:08
## 12 2018-01-24, 15:40
## 13 2018-01-21, 16:33
## 14 2018-01-21, 15:38
## 15 2018-01-21, 14:57
## titles
## 1 新功能!「我的保單」10分鐘健檢好輕鬆!
## 2 【好書必讀】保險好EASY:拿回保險的主導權‧用小錢買到大保障
## 3 【公告】回覆時請針對問題給予意見
## 4 【公告】發言前請詳閱規範唷!
## 5 【健檢規劃】保單健檢,請先看我唷!
## 6 【彰化縣】 35歲 / 女 / lala / 第1級 : 購買保險
## 7 【高雄市】 0歲 / 女 / 張小恩 / 第1級 : 保單檢視
## 8 【台中市】 32歲 / 女 / Hc Hsieh / 公司經營管理 : 保單檢視
## 9 【台中市】 30歲 / 女 / Wendy Chiang / 第1級 : 保單檢視
## 10 【新北市】 25歲 / 女 / Irshita Gan / 公司內部行政作業 : 保單檢視
## 11 【基隆市】 38歲 / 男 / TingYu Lin / 資訊業硬體測試人員 : 保單檢視
## 12 【台北市】 34歲 / 女 / Chris Wu / 公司內部行政作業 : 保單檢視
## 13 【新北市】 25歲 / 男 / Mark / 第3級 : 購買保險
## 14 【台北市】 34歲 / 女 / Jessica / 公司內部行政作業 : 保單檢視
## 15 【新北市】 2歲 / 男 / CAR / 孩童 : 保單檢視
## urls
## 1 http://forum.i835.com.tw/announces/10-t5047.html
## 2 http://forum.i835.com.tw/announces/easy-t4224.html
## 3 http://forum.i835.com.tw/announces/topic-t2271.html
## 4 http://forum.i835.com.tw/announces/topic-t1633.html
## 5 http://forum.i835.com.tw/forum-f27/topic-t636.html
## 6 http://forum.i835.com.tw/forum-f27/35-lala-1-t5727.html
## 7 http://forum.i835.com.tw/forum-f27/0-1-t5703.html
## 8 http://forum.i835.com.tw/forum-f27/32-hc-hsieh-t5709.html
## 9 http://forum.i835.com.tw/forum-f27/30-wendy-chiang-1-t5715.html
## 10 http://forum.i835.com.tw/forum-f27/25-irshita-gan-t5716.html
## 11 http://forum.i835.com.tw/forum-f27/38-tingyu-lin-t5718.html
## 12 http://forum.i835.com.tw/forum-f27/34-chris-wu-t5717.html
## 13 http://forum.i835.com.tw/forum-f27/25-mark-3-t5702.html
## 14 http://forum.i835.com.tw/forum-f27/34-jessica-t5697.html
## 15 http://forum.i835.com.tw/forum-f27/2-car-t5700.html
資料儲存
library(RSQLite)
## Warning: package 'RSQLite' was built under R version 3.4.3
con <- dbConnect(SQLite(),'test.sqlite')
data(iris)
dbWriteTable(con, 'iris', iris, overwrite= TRUE)
dbListTables(con)
## [1] "applenews" "iris"
dbDisconnect(con)
Query Data
con <- dbConnect(SQLite(),'test.sqlite')
rs <- dbSendQuery(con, 'select * from iris')
d1 <- fetch(rs, n = 10) # extract data in chunks of 10 rows
dbHasCompleted(rs)
## [1] FALSE
d2 <- fetch(rs, n = -1) # extract all remaining data
dbHasCompleted(rs)
## [1] TRUE
dbClearResult(rs)
d2
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5.4 3.7 1.5 0.2 setosa
## 2 4.8 3.4 1.6 0.2 setosa
## 3 4.8 3.0 1.4 0.1 setosa
## 4 4.3 3.0 1.1 0.1 setosa
## 5 5.8 4.0 1.2 0.2 setosa
## 6 5.7 4.4 1.5 0.4 setosa
## 7 5.4 3.9 1.3 0.4 setosa
## 8 5.1 3.5 1.4 0.3 setosa
## 9 5.7 3.8 1.7 0.3 setosa
## 10 5.1 3.8 1.5 0.3 setosa
## 11 5.4 3.4 1.7 0.2 setosa
## 12 5.1 3.7 1.5 0.4 setosa
## 13 4.6 3.6 1.0 0.2 setosa
## 14 5.1 3.3 1.7 0.5 setosa
## 15 4.8 3.4 1.9 0.2 setosa
## 16 5.0 3.0 1.6 0.2 setosa
## 17 5.0 3.4 1.6 0.4 setosa
## 18 5.2 3.5 1.5 0.2 setosa
## 19 5.2 3.4 1.4 0.2 setosa
## 20 4.7 3.2 1.6 0.2 setosa
## 21 4.8 3.1 1.6 0.2 setosa
## 22 5.4 3.4 1.5 0.4 setosa
## 23 5.2 4.1 1.5 0.1 setosa
## 24 5.5 4.2 1.4 0.2 setosa
## 25 4.9 3.1 1.5 0.2 setosa
## 26 5.0 3.2 1.2 0.2 setosa
## 27 5.5 3.5 1.3 0.2 setosa
## 28 4.9 3.6 1.4 0.1 setosa
## 29 4.4 3.0 1.3 0.2 setosa
## 30 5.1 3.4 1.5 0.2 setosa
## 31 5.0 3.5 1.3 0.3 setosa
## 32 4.5 2.3 1.3 0.3 setosa
## 33 4.4 3.2 1.3 0.2 setosa
## 34 5.0 3.5 1.6 0.6 setosa
## 35 5.1 3.8 1.9 0.4 setosa
## 36 4.8 3.0 1.4 0.3 setosa
## 37 5.1 3.8 1.6 0.2 setosa
## 38 4.6 3.2 1.4 0.2 setosa
## 39 5.3 3.7 1.5 0.2 setosa
## 40 5.0 3.3 1.4 0.2 setosa
## 41 7.0 3.2 4.7 1.4 versicolor
## 42 6.4 3.2 4.5 1.5 versicolor
## 43 6.9 3.1 4.9 1.5 versicolor
## 44 5.5 2.3 4.0 1.3 versicolor
## 45 6.5 2.8 4.6 1.5 versicolor
## 46 5.7 2.8 4.5 1.3 versicolor
## 47 6.3 3.3 4.7 1.6 versicolor
## 48 4.9 2.4 3.3 1.0 versicolor
## 49 6.6 2.9 4.6 1.3 versicolor
## 50 5.2 2.7 3.9 1.4 versicolor
## 51 5.0 2.0 3.5 1.0 versicolor
## 52 5.9 3.0 4.2 1.5 versicolor
## 53 6.0 2.2 4.0 1.0 versicolor
## 54 6.1 2.9 4.7 1.4 versicolor
## 55 5.6 2.9 3.6 1.3 versicolor
## 56 6.7 3.1 4.4 1.4 versicolor
## 57 5.6 3.0 4.5 1.5 versicolor
## 58 5.8 2.7 4.1 1.0 versicolor
## 59 6.2 2.2 4.5 1.5 versicolor
## 60 5.6 2.5 3.9 1.1 versicolor
## 61 5.9 3.2 4.8 1.8 versicolor
## 62 6.1 2.8 4.0 1.3 versicolor
## 63 6.3 2.5 4.9 1.5 versicolor
## 64 6.1 2.8 4.7 1.2 versicolor
## 65 6.4 2.9 4.3 1.3 versicolor
## 66 6.6 3.0 4.4 1.4 versicolor
## 67 6.8 2.8 4.8 1.4 versicolor
## 68 6.7 3.0 5.0 1.7 versicolor
## 69 6.0 2.9 4.5 1.5 versicolor
## 70 5.7 2.6 3.5 1.0 versicolor
## 71 5.5 2.4 3.8 1.1 versicolor
## 72 5.5 2.4 3.7 1.0 versicolor
## 73 5.8 2.7 3.9 1.2 versicolor
## 74 6.0 2.7 5.1 1.6 versicolor
## 75 5.4 3.0 4.5 1.5 versicolor
## 76 6.0 3.4 4.5 1.6 versicolor
## 77 6.7 3.1 4.7 1.5 versicolor
## 78 6.3 2.3 4.4 1.3 versicolor
## 79 5.6 3.0 4.1 1.3 versicolor
## 80 5.5 2.5 4.0 1.3 versicolor
## 81 5.5 2.6 4.4 1.2 versicolor
## 82 6.1 3.0 4.6 1.4 versicolor
## 83 5.8 2.6 4.0 1.2 versicolor
## 84 5.0 2.3 3.3 1.0 versicolor
## 85 5.6 2.7 4.2 1.3 versicolor
## 86 5.7 3.0 4.2 1.2 versicolor
## 87 5.7 2.9 4.2 1.3 versicolor
## 88 6.2 2.9 4.3 1.3 versicolor
## 89 5.1 2.5 3.0 1.1 versicolor
## 90 5.7 2.8 4.1 1.3 versicolor
## 91 6.3 3.3 6.0 2.5 virginica
## 92 5.8 2.7 5.1 1.9 virginica
## 93 7.1 3.0 5.9 2.1 virginica
## 94 6.3 2.9 5.6 1.8 virginica
## 95 6.5 3.0 5.8 2.2 virginica
## 96 7.6 3.0 6.6 2.1 virginica
## 97 4.9 2.5 4.5 1.7 virginica
## 98 7.3 2.9 6.3 1.8 virginica
## 99 6.7 2.5 5.8 1.8 virginica
## 100 7.2 3.6 6.1 2.5 virginica
## 101 6.5 3.2 5.1 2.0 virginica
## 102 6.4 2.7 5.3 1.9 virginica
## 103 6.8 3.0 5.5 2.1 virginica
## 104 5.7 2.5 5.0 2.0 virginica
## 105 5.8 2.8 5.1 2.4 virginica
## 106 6.4 3.2 5.3 2.3 virginica
## 107 6.5 3.0 5.5 1.8 virginica
## 108 7.7 3.8 6.7 2.2 virginica
## 109 7.7 2.6 6.9 2.3 virginica
## 110 6.0 2.2 5.0 1.5 virginica
## 111 6.9 3.2 5.7 2.3 virginica
## 112 5.6 2.8 4.9 2.0 virginica
## 113 7.7 2.8 6.7 2.0 virginica
## 114 6.3 2.7 4.9 1.8 virginica
## 115 6.7 3.3 5.7 2.1 virginica
## 116 7.2 3.2 6.0 1.8 virginica
## 117 6.2 2.8 4.8 1.8 virginica
## 118 6.1 3.0 4.9 1.8 virginica
## 119 6.4 2.8 5.6 2.1 virginica
## 120 7.2 3.0 5.8 1.6 virginica
## 121 7.4 2.8 6.1 1.9 virginica
## 122 7.9 3.8 6.4 2.0 virginica
## 123 6.4 2.8 5.6 2.2 virginica
## 124 6.3 2.8 5.1 1.5 virginica
## 125 6.1 2.6 5.6 1.4 virginica
## 126 7.7 3.0 6.1 2.3 virginica
## 127 6.3 3.4 5.6 2.4 virginica
## 128 6.4 3.1 5.5 1.8 virginica
## 129 6.0 3.0 4.8 1.8 virginica
## 130 6.9 3.1 5.4 2.1 virginica
## 131 6.7 3.1 5.6 2.4 virginica
## 132 6.9 3.1 5.1 2.3 virginica
## 133 5.8 2.7 5.1 1.9 virginica
## 134 6.8 3.2 5.9 2.3 virginica
## 135 6.7 3.3 5.7 2.5 virginica
## 136 6.7 3.0 5.2 2.3 virginica
## 137 6.3 2.5 5.0 1.9 virginica
## 138 6.5 3.0 5.2 2.0 virginica
## 139 6.2 3.4 5.4 2.3 virginica
## 140 5.9 3.0 5.1 1.8 virginica
res <- dbGetQuery(con, 'SELECT Species, count(*) FROM iris group by Species')
res
## Species count(*)
## 1 setosa 50
## 2 versicolor 50
## 3 virginica 50
dbWriteTable(con, "applenews", applenews, overwrite = TRUE)
dbListTables(con)
## [1] "applenews" "iris"
df <- dbReadTable(con,"applenews")
#df
res <- dbGetQuery(con, "SELECT category, count(*) FROM applenews group by category order by count(*) desc")
#res
names(res) = c('category', 'cnt')
pie(res$cnt,labels = res$category, init.angle = 90, clockwise = TRUE)

停車費查詢
library(httr)
url <- 'http://parkingfee.pma.gov.tw/Home/Index/1028'
payload <- list(
CarID = 'ARR-8888',
CarType = 'C'
)
res <- POST(url, body = payload, encode = 'form')
content(res) %>% html_nodes('.tableStyle1 td') %>% html_text()
## [1] "停車費明細(傳回筆數: 0)"
## [2] "目前無資料"
## [3] "\r\n \r\n <U+00AB><U+00BB>\r\n \r\n\r\n "
抓取證交所資料
library(jsonlite)
twse <- fromJSON('http://www.twse.com.tw/exchangeReport/MI_INDEX?response=json&date=&type=&_=1516860844466')
df <- twse$data1
colnames(df) <- twse$fields1
head(df)
## 指數 收盤指數 漲跌(+/-)
## [1,] "寶島股價指數" "12,846.98" "<p style ='color:red'>+</p>"
## [2,] "發行量加權股價指數" "11,165.95" "<p style ='color:red'>+</p>"
## [3,] "臺灣公司治理100指數" "6,376.64" "<p style ='color:red'>+</p>"
## [4,] "臺灣50指數" "8,487.00" "<p style ='color:red'>+</p>"
## [5,] "臺灣中型100指數" "8,032.53" "<p style ='color:green'>-</p>"
## [6,] "臺灣資訊科技指數" "11,185.70" "<p style ='color:green'>-</p>"
## 漲跌點數 漲跌百分比(%)
## [1,] "3.72" "0.03"
## [2,] "13.79" "0.12"
## [3,] "19.06" "0.30"
## [4,] "26.15" "0.31"
## [5,] "45.85" "-0.57"
## [6,] "21.15" "-0.19"
Quantmod
library(quantmod)
## Warning: package 'quantmod' was built under R version 3.4.3
## Loading required package: xts
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
## Loading required package: TTR
## Version 0.4-0 included new data defaults. See ?getSymbols.
getSymbols('2330.TW')
## 'getSymbols' currently uses auto.assign=TRUE by default, but will
## use auto.assign=FALSE in 0.5-0. You will still be able to use
## 'loadSymbols' to automatically load data. getOption("getSymbols.env")
## and getOption("getSymbols.auto.assign") will still be checked for
## alternate defaults.
##
## This message is shown once per session and may be disabled by setting
## options("getSymbols.warning4.0"=FALSE). See ?getSymbols for details.
##
## WARNING: There have been significant changes to Yahoo Finance data.
## Please see the Warning section of '?getSymbols.yahoo' for details.
##
## This message is shown once per session and may be disabled by setting
## options("getSymbols.yahoo.warning"=FALSE).
## Warning: 2330.TW contains missing values. Some functions will not work if
## objects contain missing values in the middle of the series. Consider using
## na.omit(), na.approx(), na.fill(), etc to remove or replace them.
## [1] "2330.TW"
chartSeries(`2330.TW`)

addTA(SMA(na.omit(`2330.TW`$`2330.TW.Close`)))

抓取104 資料
library(rvest)
getJobs <- function(keyword, page){
url <- paste0('https://www.104.com.tw/jobs/search/?ro=0&keyword=',keyword,'&order=1&asc=0&kwop=7&page=', page , '&mode=s&jobsource=n104bank1')
articles <- read_html(url) %>% html_nodes('article.job-list-item')
jobtitle <- articles %>% html_nodes('.js-job-link') %>% html_text()
company <- articles %>% html_attr('data-cust-name')
cate <- articles %>% html_attr('data-indcat-desc')
area <- unlist(lapply(articles, function(e) e %>% html_nodes('.job-list-intro li') %>% .[1] %>% html_text()) )
exp <- unlist(lapply(articles, function(e) e %>% html_nodes('.job-list-intro li') %>% .[2] %>% html_text()) )
edu <- unlist(lapply(articles, function(e) e %>% html_nodes('.job-list-intro li') %>% .[3] %>% html_text()) )
jobs <- data.frame(company, cate, area, exp, edu, stringsAsFactors = FALSE)
jobs
}
dfall <- data.frame()
for (i in 1:3){
df<- getJobs('資料科學家' , i)
dfall <- rbind(dfall, df)
}
head(dfall)
## company cate
## 1 國泰人壽保險股份有限公司_總公司_國泰金控 人身保險業
## 2 烏龜移動科技股份有限公司 廣告行銷公關業
## 3 行動基因生技股份有限公司 生化科技研發業
## 4 香港商傑風網路有限公司台灣分公司 電子通訊/電腦週邊零售業
## 5 雲智匯科技服務股份有限公司 其他電子零組件相關業
## 6 思納捷科技股份有限公司 電腦軟體服務業
## area exp edu
## 1 台北市大安區 經歷不拘 碩士
## 2 台北市松山區 經歷不拘 大學
## 3 台北市內湖區 4年以上 博士
## 4 台北市大安區 經歷不拘 專科
## 5 廣東省深圳市 3年以上 碩士
## 6 台北市松山區 1年以上 碩士
barplot(sort(table(dfall$cate), decreasing = TRUE))

barplot(sort(table(dfall$area), decreasing = TRUE))

barplot(sort(table(dfall$exp), decreasing = TRUE))

barplot(sort(table(dfall$edu), decreasing = TRUE))
