為了使用這些套件,我們必須先安裝它們。
install.packages("XML")
##
## The downloaded binary packages are in
## /var/folders/5c/0p5zr2_n4xvbt2j6hkqczhph0000gn/T//RtmpUI1qaF/downloaded_packages
library("XML")
That's it! You are ready for building simple web-crawler now.
眼見為憑,我們用一個小例子示範一下。
This way to TWSE
MOPS_URL.TWSE_ALL <-
"http://www.twse.com.tw/en/listed/listed_company/apply_listing.php?page=1"
web_page <- htmlParse(MOPS_URL.TWSE_ALL,encoding="big5")
data <- readHTMLTable(web_page, which=6, stringsAsFactors=F, header = T)
names(data) <-
c("Application Date", "Code", "Company", "Chairman","Amount of Capital",
"Underwriter")
data <- data[-1,]
head(data, n=3)
## Application Date Code Company Chairman Amount of Capital Underwriter
## 2 2015.03.03 6442 Ezconn 600,000
## 3 2014.10.16 3416 WinMate 610,664
## 4 2014.10.07 8341 SF 1,000,000
Magic!
不要問....很可怕 = =+
簡而言之,一般網路的運作方式大致上入下圖所示:
常見的 URL 形態:
Try it!
常見的資料形態:
<html>
<head>
<meta></meta>
<title></title>
<style type="text/css"></style>
</head>
<body>
<h1></h1>
<section></section>
<footer></footer>
</body>
</html>
以 PChome 網路商城為例
破解後台
以這個後台的 URL 來說,你可以猜到它代表的意義嗎?
在 Ajax 技術一章會有進一步的介紹。
用 PostMan 把這個 csv 下載下來!
What does a parsor do in a spider?
rm(list=ls())
MOPS_URL.TWSE_ALL <-
"http://www.twse.com.tw/en/listed/listed_company/apply_listing.php?page=1"
web_page <- htmlParse(MOPS_URL.TWSE_ALL,encoding="big5")
data <- readHTMLTable(web_page, which=6, stringsAsFactors=F, header = T)
names(data) <-
c("Application Date", "Code", "Company", "Chairman","Amount of Capital",
"Underwriter")
data <- data[-1,]
head(data, n=3)
(s <- "I love R!") # This is string
(b <- T) # Boolean
(f <- pi) # Floating number (real number).
(i <- 3) # Integer
## [1] "I love R!"
## [1] TRUE
## [1] 3.142
## [1] 3
v1 <- c(1, 2, 3) # c() is the concatenate function in R.
v2 <- c(2, 3)
(r1 <- v1 + v2)
## Warning: 較長的物件長度並非較短物件長度的倍數
(r2 <- v1 * v2)
## Warning: 較長的物件長度並非較短物件長度的倍數
## [1] 3 5 5
## [1] 2 6 6
c() can be used to combine vectors into one vector.
c(v1, v2)
## [1] 1 2 3 2 3
All elements in a vector must be of the same type
v <- c(1, 2, "3")
v
## [1] "1" "2" "3"
List, different from vector, can be used for storing data of different types.
c3h3 <- list(name="c3h3", age=30, height=172, weight=65.3)
c3h3
## $name
## [1] "c3h3"
##
## $age
## [1] 30
##
## $height
## [1] 172
##
## $weight
## [1] 65.3
c() can be also used for combining lists.
c3h3 <- c(c3h3, list(zip_code='100'))
c3h3
## $name
## [1] "c3h3"
##
## $age
## [1] 30
##
## $height
## [1] 172
##
## $weight
## [1] 65.3
##
## $zip_code
## [1] "100"
require(gdata)
domestic_ins_company <- read.xls("http://www.tii.org.tw/images_P2/%E6%9C%AC%E5%9C%8B%E7%94%A2%E9%9A%AA%E5%85%AC%E5%8F%B8_20140822.xls")
(names(domestic_ins_company))
head(domestic_ins_company, n=2)
## [1] "名.稱.與.所.在.地" "設立日期" "董事長"
## [4] "總經理" "電話.電傳" "網址"
## 名.稱.與.所.在.地
## 1
## 2 臺灣產物保險股份有限公司\n100 台北市館前路49號 8、9樓\nTaiwan Fire & Marine Insurance Co., Ltd\n8-9 F1., No.49, Kuan Chien Road, Taipei 100
## 設立日期 董事長 總經理
## 1
## 2 37/03/12 李泰宏\nSteve Lee 宋道平\nCharles Song
## 電話.電傳
## 1 與免付費電話
## 2 Tel. (02)23821666\nFax. (02)23882555\n0809-068888
## 網址
## 1 (公司、資訊公開與重大訊息)
## 2 http://www.tfmi.com.tw/
domestic_ins_company
是直接從金管會業務統計資料中的保險機構一覽表中拿到的本國保險機構資料。
read.xls
就是扮演著 parsor 的角色,讓你可以把資料讀進 R 中做進一步的處理。
我有 8 個網址要爬,難不成我要複製貼上 8 次 read.xls
!?
This way to the website
urls <- list("http://www.tii.org.tw/images_P2/%E6%9C%AC%E5%9C%8B%E7%94%A2%E9%9A%AA%E5%85%AC%E5%8F%B8_20140822.xls",
"http://www.tii.org.tw/images_P2/%E5%A4%96%E5%9C%8B%E7%94%A2%E9%9A%AA%E5%85%AC%E5%8F%B8_20141030.xls",
"http://www.tii.org.tw/images_P2/%E6%9C%AC%E5%9C%8B%E5%A3%BD%E9%9A%AA%E5%85%AC%E5%8F%B8_20150113.xls",
"http://www.tii.org.tw/images_P2/%E5%A4%96%E5%9C%8B%E5%A3%BD%E9%9A%AA%E5%85%AC%E5%8F%B8_20140904.xls",
"http://www.tii.org.tw/images_P2/%E5%B0%88%E6%A5%AD%E5%86%8D%E4%BF%9D%E9%9A%AA%E5%85%AC%E5%8F%B81001228.xls",
"http://www.tii.org.tw/images_P2/%E5%A4%96%E5%9C%8B%E4%BF%9D%E9%9A%AA%E5%85%AC%E5%8F%B8%E5%9C%A8%E5%8F%B0%E8%81%AF%E7%B5%A1%E8%99%9520140703.xls",
"http://www.tii.org.tw/images_P2/%E6%9C%AC%E5%9C%8B%E4%BF%9D%E9%9A%AA%E7%9B%B8%E9%97%9C%E6%A9%9F%E6%A7%8B_20141227.xls",
"http://www.tii.org.tw/images_P2/%E5%A4%96%E5%9C%8B%E9%87%91%E8%9E%8D%E4%BF%9D%E9%9A%AA%E7%9B%B8%E9%97%9C%E6%A9%9F%E6%A7%8B1010102.xls")
results <- list()
for (url in urls){
print(paste("Processing ", url))
data <- read.xls(url, header=T, stringsAsFactors=F)
results <- c(results, list(data))
}
## [1] "Processing http://www.tii.org.tw/images_P2/%E6%9C%AC%E5%9C%8B%E7%94%A2%E9%9A%AA%E5%85%AC%E5%8F%B8_20140822.xls"
## [1] "Processing http://www.tii.org.tw/images_P2/%E5%A4%96%E5%9C%8B%E7%94%A2%E9%9A%AA%E5%85%AC%E5%8F%B8_20141030.xls"
## [1] "Processing http://www.tii.org.tw/images_P2/%E6%9C%AC%E5%9C%8B%E5%A3%BD%E9%9A%AA%E5%85%AC%E5%8F%B8_20150113.xls"
## [1] "Processing http://www.tii.org.tw/images_P2/%E5%A4%96%E5%9C%8B%E5%A3%BD%E9%9A%AA%E5%85%AC%E5%8F%B8_20140904.xls"
## [1] "Processing http://www.tii.org.tw/images_P2/%E5%B0%88%E6%A5%AD%E5%86%8D%E4%BF%9D%E9%9A%AA%E5%85%AC%E5%8F%B81001228.xls"
## [1] "Processing http://www.tii.org.tw/images_P2/%E5%A4%96%E5%9C%8B%E4%BF%9D%E9%9A%AA%E5%85%AC%E5%8F%B8%E5%9C%A8%E5%8F%B0%E8%81%AF%E7%B5%A1%E8%99%9520140703.xls"
## [1] "Processing http://www.tii.org.tw/images_P2/%E6%9C%AC%E5%9C%8B%E4%BF%9D%E9%9A%AA%E7%9B%B8%E9%97%9C%E6%A9%9F%E6%A7%8B_20141227.xls"
## [1] "Processing http://www.tii.org.tw/images_P2/%E5%A4%96%E5%9C%8B%E9%87%91%E8%9E%8D%E4%BF%9D%E9%9A%AA%E7%9B%B8%E9%97%9C%E6%A9%9F%E6%A7%8B1010102.xls"
head(results[[1]], n = 3)
## 名.稱.與.所.在.地
## 1
## 2 臺灣產物保險股份有限公司\n100 台北市館前路49號 8、9樓\nTaiwan Fire & Marine Insurance Co., Ltd\n8-9 F1., No.49, Kuan Chien Road, Taipei 100
## 3
## 設立日期 董事長 總經理
## 1
## 2 37/03/12 李泰宏\nSteve Lee 宋道平\nCharles Song
## 3
## 電話.電傳
## 1 與免付費電話
## 2 Tel. (02)23821666\nFax. (02)23882555\n0809-068888
## 3
## 網址
## 1 (公司、資訊公開與重大訊息)
## 2 http://www.tfmi.com.tw/
## 3 http://www.tfmi.com.tw/ec/news/news-public.screen
results[[1]]
是什麼?
[index1[, index2, ...]]
之類的語法來存取特定區塊的資料。稱之為 slicing。
v <- c(1:20) # v 是個 vector
l <- list(pi, 306, "Taishin") # l 是個 list
df <- data(iris) # df 是個 data.frame
print(c("v[3]", v[3])) # 抽取 v 的第三個 element
print(c(class(l[3]), class(l[[3]]))) # [] 與 [[]] 的差別
print(iris[c(2, 5), c(1, 3, 5)]) # data.frame 的 slicing 語法
## [1] "v[3]" "3"
## [1] "list" "character"
## Sepal.Length Petal.Length Species
## 2 4.9 1.4 setosa
## 5 5.0 1.4 setosa
Dataframe 長得像這樣子 (以 iris 為例)
irirs[c(2, 5), c(1, 3, 5)]
或者是
iris[c(2, 5), c("Sepal.Length", "Petal.Length", "Species")]
head(df, n=6)
顧名思義,會顯示 df 前 n 筆資料
head(iris) # 預設 n 是 6 筆
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5.0 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa
tail(df, n=6)
跟 head()
成對,會顯示 df 最後 n 筆資料
tail(iris) # 預設一樣是 6
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 145 6.7 3.3 5.7 2.5 virginica
## 146 6.7 3.0 5.2 2.3 virginica
## 147 6.3 2.5 5.0 1.9 virginica
## 148 6.5 3.0 5.2 2.0 virginica
## 149 6.2 3.4 5.4 2.3 virginica
## 150 5.9 3.0 5.1 1.8 virginica
除了 list 可以取名字、data.frame 有欄位名,vector 也是可以取名字並且用 []
取存取它!
weights <- c(hsiang=70, c3h3=62, dboy=82)
weights["c3h3"]
## c3h3
## 62
[]
與 [[]]
是不一樣的
class(c3h3["weight"]) # 是個 list
class(c3h3[["weight"]]) # 是個數字
## [1] "list"
## [1] "numeric"
回頭看看之前抓到的資料
(head(results[[1]], n=2))
## 名.稱.與.所.在.地
## 1
## 2 臺灣產物保險股份有限公司\n100 台北市館前路49號 8、9樓\nTaiwan Fire & Marine Insurance Co., Ltd\n8-9 F1., No.49, Kuan Chien Road, Taipei 100
## 設立日期 董事長 總經理
## 1
## 2 37/03/12 李泰宏\nSteve Lee 宋道平\nCharles Song
## 電話.電傳
## 1 與免付費電話
## 2 Tel. (02)23821666\nFax. (02)23882555\n0809-068888
## 網址
## 1 (公司、資訊公開與重大訊息)
## 2 http://www.tfmi.com.tw/
?command
去查詢相關文件與指令說明。
?c # 查詢 c 的說明文件
??c # 搜尋所有有關 c 的說明文件
read.csv