SlideShare uma empresa Scribd logo
1 de 60
Baixar para ler offline
20130325 MLDM Monday

  R 上的 spideR 寫作軍火庫


        by c3h3 
TW useR Group & MLDM Monday

●   http://www.meetup.com/Taiwan-useR-Group/
●   http://www.facebook.com/TaiwanUseRGroup/
●   http://www.youtube.com/user/TWuseRGroup/
●   http://tw.use-r.net/
關於講者

●   Chia-Chi Chang (c3h3)
●   Chief of Data Scientist of InnovoTECH
●   TW useR Group / MLDM Monday 創辦人
    之一
●   R 、Python 和 Maple 的愛用者
●   平時喜歡分析各種類型的資料、買賣金融商
    品;另外,也喜歡閱讀各種數學理論、模型、以
    及它們的應用......
講題大綱

●   spideR   寫作的預備知識
●   spideR   的一些小範例
●   spideR   的架構
●   spideR   的寫作流程
●   spideR   的一些小技巧
本次演講適合初學者
請各位高手們忍耐一下囉!
預備知識
spideR 寫作的預備知識

●   什麼是網站?
●   網站的結構?
●   網址的祕密?
●   網站資料的種類?
●   分析的工具
什麼是網站?
一般人眼中的網站
設計師眼中的網站
工程師眼中的網站
那... spideR 眼中的網站呢?
網站的結構?
網站的結構 (分類)

●   前端 V.S. 後端
●   Model + View + Controler
    (MVC)
●   Static V.S. Dynamic (Ajax)
MVC結構
Static V.S. Dynamic (Ajax)

●   範例:
●   [Ajax] http://shop.
    myer.com.
    au/shop/mystore/9
    73607510
●   [Static] http://tw.
    stock.yahoo.
    com/d/s/major_24
    51.html
網址的祕密?
網址的祕密

● URL?var_1=val_1&var_2=val_2...
  ○ 其實,就像呼叫函數一樣
  ○ 相關訊息可在 form 中或 JS code 中找到
  ○ http://finance.yahoo.com/q/hp?s=%
     5ETWII&a=06&b=2&c=1997&d=02&e=24&f=2013&g=d
 ○   http://www.taifex.com.tw/eng/eng3/eng3_2dl.asp?
     COMMODITY_ID=all&DATA_DATE=2012/11/01&DATA_D
     ATE1=2012/11/15
網址的祕密

● URL 中帶有規則
  ○ 有些網址會把訊息藏在 URL 中
  ○ 然後,在由後端的 URL Dispatcher 解析

● URL 中帶有規則的範例:
  ○ http://tw.stock.yahoo.
    com/d/s/major_2451.html
  ○ URL規則: major_StockID.html
網站資料的種類?
網站資料的種類?

● Page (HTML)
● Data (JSON/XML...)
● File
網站資料 Data (JSON/XML...)
網站資料 File
常用的工具
常用的工具

● Google Chrome
  ○ Developer Tools
● Firefox
  ○ Firebug
  ○ Hackbar
  ○ Cookie Manager+
● cURL
● Wireshark
一些小範例
[Example1] 抓股票代碼:
使用技術

● Example1_Extract_TWSE_Stock_IDs.R
● R
  ○ XML::htmlParse
  ○ XML::readHTMLTable
  ○ charToRaw
  ○ gsub
● Reference:
  ○ [共筆Blog] 去除 " " 的方法
  ○ R 的 regular expresssion 講義
[Example2] 抓取大戶進出:
使用技術

● Example2_Extract_Stock_Major_Data_
  Fom_Kimo.R
● R
  ○ XML::htmlParse
  ○ XML::readHTMLTable
回家作業:
● 綜合前兩個範例:
  ○ 抓取全部代碼的 ID
  ○ 抓取 OTC 的資料
    ■ Hint: OTC_IDs
  ○ 將所不同 ID 的 Data Table 用不同名稱命名
    ■ Hint1: 可以讓函數 output Data Table
    ■ Hint2: 也可以用 assign 函數
  ○ 在 Data Table 中使用一個新欄位來存 ID ===> 建
   立總表
 ○ 在 Data Table 中使用一個新欄位來存日期
[Example3] 抓取0050代碼:
使用技術

● Example3_Extract_0050_IDs.R
● R
  ○ XML::htmlParse
  ○ XPath Parser in XML

● Reference:
  ○ http://www.w3.org/TR/xpath/
[Example4] 利用 ID 搭配
quantmod:
使用技術
● Example4_Get_Stock_Data_From_Yahoo
  Finance.R
● R
  ○ quantmod::getSymbols
  ○ quantmod::chartSeries
  ○ get
  ○ assign
● Reference:
  ○ Quantmod Web
  ○ Quantmod Slide
[Example5] 找到後台的JSON時?
回家作業:
● 可以利用 R 中的 rjson 套件,練習處理看看
  賞面的網頁?
● Reference:
  ○ rjson: http://cran.r-project.
    org/web/packages/rjson/rjson.pdf
[Example6] 當遇到下載檔案時
使用技術
● Example6_Download_CSV_File_From_T
  WSE.R
● R
  ○ RCurl::getURL
  ○ file
    ■ writeLines
    ■ readLines
  ○ textConnection
  ○ read.table
回家作業:
● 接續上方範例......
  ○ 運用 apply 對每一行都 parse 開
  ○ 利用長度去掉不要的資料
  ○ 把留下的資料運用 do.call(rbind,
    data_list) 合成
  ○ 然後,製作成Data frame格式並存入
    RData 檔案之中
[Example7] 看code學寫code
[Example7] 下載zip檔
使用技術

● Example7_Download_ZIP_File_From_Tai
  fex.R

● R
  ○ download.file
  ○ unzip
[Example8] 當遇需要 Cookie 時
使用技術

● Example8_Download_CSV_File_From_Tai
  fex_With_Cookie.R

● R
  ○   RCurl::getCurlHandle
  ○   RCurl::getURL(url,curl=curlHandle)
  ○   XML::htmlParse
  ○   XML::xmlAttrs
回家作業:
● 接續上方範例......
  ○ 練習用 readline 讀入 unzip 出來的 rpt 檔
  ○ 並將 rpt 檔轉換成 quantmod 可以分析用
    的 xts 格式
spideR 的架構
spideR 的架構

● Web Connector
  ○ RCurl
● Data Parser (Cleaner)
  ○ XML
● Data Center
    ○ RData File
    ○ DB (SQLite, MySQL, PostgreSQL,
spideR 的寫作流程
spideR 的寫作流程
●   確立目標?
●   觀察網頁
●   頁面分類
●   分類頁面的 Connector 實作
●   分類頁面的 Parser 實作
●   資料庫比對與存取
一些小技巧
尋找「後台」的小技巧1 -- 監控
尋找「後台」的小技巧2 -- 找form
尋找「資料」的小技巧1
  打開 hidden
尋找「資料」的小技巧2
  利用JQuery
尋找「資料」的小技巧3
 利用 JS debugger;
尋找「資料」的小技巧4
 停用 JS (停用前)
尋找「資料」的小技巧4
停用 JS (停用後:推薦商品消失)
Q&A
感謝大家

Mais conteúdo relacionado

Semelhante a 20130325 mldm monday spide r

The Evolution of Data Systems
The Evolution of Data SystemsThe Evolution of Data Systems
The Evolution of Data Systems宇 傅
 
[DCTPE2011] 11) Drupal 是好的生財工具嗎? 1. 網站標案經驗分享 x 2
[DCTPE2011] 11) Drupal 是好的生財工具嗎?  1. 網站標案經驗分享 x 2[DCTPE2011] 11) Drupal 是好的生財工具嗎?  1. 網站標案經驗分享 x 2
[DCTPE2011] 11) Drupal 是好的生財工具嗎? 1. 網站標案經驗分享 x 2Drupal Taiwan
 
認試軟體測試的世界 & TDD/BDD 入門
認試軟體測試的世界 & TDD/BDD 入門認試軟體測試的世界 & TDD/BDD 入門
認試軟體測試的世界 & TDD/BDD 入門wantingj
 
Django development
Django developmentDjango development
Django developmentloveyudu
 
初探 Elastic Observability 的實踐方法
初探 Elastic Observability 的實踐方法初探 Elastic Observability 的實踐方法
初探 Elastic Observability 的實踐方法Joe Wu
 
20121115 Slides
20121115 Slides20121115 Slides
20121115 SlidesTonyq Wang
 
1.2 刘奇 go在分布式数据库中的应用
1.2 刘奇 go在分布式数据库中的应用1.2 刘奇 go在分布式数据库中的应用
1.2 刘奇 go在分布式数据库中的应用Leo Zhou
 
在生命轉彎的地方 - 從軟體開發職涯,探索人生
在生命轉彎的地方 - 從軟體開發職涯,探索人生在生命轉彎的地方 - 從軟體開發職涯,探索人生
在生命轉彎的地方 - 從軟體開發職涯,探索人生Rick Hwang
 
SRE Study Notes - CH2,3,4
SRE Study Notes - CH2,3,4SRE Study Notes - CH2,3,4
SRE Study Notes - CH2,3,4Rick Hwang
 
Python网络抓取小试
Python网络抓取小试Python网络抓取小试
Python网络抓取小试greatghoul
 
Django入门
Django入门Django入门
Django入门oikomi
 
SRE Study Notes - CH2,3,4
SRE Study Notes - CH2,3,4SRE Study Notes - CH2,3,4
SRE Study Notes - CH2,3,4Rick Hwang
 
[DCTPE2011] Drupal 6 的 CCK/Views運用--林振昇
[DCTPE2011] Drupal 6 的 CCK/Views運用--林振昇[DCTPE2011] Drupal 6 的 CCK/Views運用--林振昇
[DCTPE2011] Drupal 6 的 CCK/Views運用--林振昇Drupal Taiwan
 
基于Tornado后端系统架构暨最佳实践
基于Tornado后端系统架构暨最佳实践基于Tornado后端系统架构暨最佳实践
基于Tornado后端系统架构暨最佳实践ZY Zhang
 
Deep learning hardware architecture and software deploy with docker
Deep learning hardware architecture and software deploy with dockerDeep learning hardware architecture and software deploy with docker
Deep learning hardware architecture and software deploy with dockerYa-Lun Li
 
Node.js從無到有 基本課程
Node.js從無到有 基本課程Node.js從無到有 基本課程
Node.js從無到有 基本課程Simon Su
 
Clojure and FP
Clojure and FPClojure and FP
Clojure and FPQin Jian
 
SRE Study Notes - Opening, CH1
SRE Study Notes - Opening, CH1SRE Study Notes - Opening, CH1
SRE Study Notes - Opening, CH1Rick Hwang
 

Semelhante a 20130325 mldm monday spide r (20)

Django step0
Django step0Django step0
Django step0
 
The Evolution of Data Systems
The Evolution of Data SystemsThe Evolution of Data Systems
The Evolution of Data Systems
 
[DCTPE2011] 11) Drupal 是好的生財工具嗎? 1. 網站標案經驗分享 x 2
[DCTPE2011] 11) Drupal 是好的生財工具嗎?  1. 網站標案經驗分享 x 2[DCTPE2011] 11) Drupal 是好的生財工具嗎?  1. 網站標案經驗分享 x 2
[DCTPE2011] 11) Drupal 是好的生財工具嗎? 1. 網站標案經驗分享 x 2
 
認試軟體測試的世界 & TDD/BDD 入門
認試軟體測試的世界 & TDD/BDD 入門認試軟體測試的世界 & TDD/BDD 入門
認試軟體測試的世界 & TDD/BDD 入門
 
Django development
Django developmentDjango development
Django development
 
Using vim
Using vimUsing vim
Using vim
 
初探 Elastic Observability 的實踐方法
初探 Elastic Observability 的實踐方法初探 Elastic Observability 的實踐方法
初探 Elastic Observability 的實踐方法
 
20121115 Slides
20121115 Slides20121115 Slides
20121115 Slides
 
1.2 刘奇 go在分布式数据库中的应用
1.2 刘奇 go在分布式数据库中的应用1.2 刘奇 go在分布式数据库中的应用
1.2 刘奇 go在分布式数据库中的应用
 
在生命轉彎的地方 - 從軟體開發職涯,探索人生
在生命轉彎的地方 - 從軟體開發職涯,探索人生在生命轉彎的地方 - 從軟體開發職涯,探索人生
在生命轉彎的地方 - 從軟體開發職涯,探索人生
 
SRE Study Notes - CH2,3,4
SRE Study Notes - CH2,3,4SRE Study Notes - CH2,3,4
SRE Study Notes - CH2,3,4
 
Python网络抓取小试
Python网络抓取小试Python网络抓取小试
Python网络抓取小试
 
Django入门
Django入门Django入门
Django入门
 
SRE Study Notes - CH2,3,4
SRE Study Notes - CH2,3,4SRE Study Notes - CH2,3,4
SRE Study Notes - CH2,3,4
 
[DCTPE2011] Drupal 6 的 CCK/Views運用--林振昇
[DCTPE2011] Drupal 6 的 CCK/Views運用--林振昇[DCTPE2011] Drupal 6 的 CCK/Views運用--林振昇
[DCTPE2011] Drupal 6 的 CCK/Views運用--林振昇
 
基于Tornado后端系统架构暨最佳实践
基于Tornado后端系统架构暨最佳实践基于Tornado后端系统架构暨最佳实践
基于Tornado后端系统架构暨最佳实践
 
Deep learning hardware architecture and software deploy with docker
Deep learning hardware architecture and software deploy with dockerDeep learning hardware architecture and software deploy with docker
Deep learning hardware architecture and software deploy with docker
 
Node.js從無到有 基本課程
Node.js從無到有 基本課程Node.js從無到有 基本課程
Node.js從無到有 基本課程
 
Clojure and FP
Clojure and FPClojure and FP
Clojure and FP
 
SRE Study Notes - Opening, CH1
SRE Study Notes - Opening, CH1SRE Study Notes - Opening, CH1
SRE Study Notes - Opening, CH1
 

Mais de Chia-Chi Chang

how to learn quantmod and quantstrat by yourself
how to learn quantmod and quantstrat by yourselfhow to learn quantmod and quantstrat by yourself
how to learn quantmod and quantstrat by yourselfChia-Chi Chang
 
Communicate with your data 20170104
Communicate with your data 20170104Communicate with your data 20170104
Communicate with your data 20170104Chia-Chi Chang
 
20161110 quantstrat in seattle
20161110 quantstrat in seattle20161110 quantstrat in seattle
20161110 quantstrat in seattleChia-Chi Chang
 
PyData SF 2016 --- Moving forward through the darkness
PyData SF 2016 --- Moving forward through the darknessPyData SF 2016 --- Moving forward through the darkness
PyData SF 2016 --- Moving forward through the darknessChia-Chi Chang
 
20160827 open community camp
20160827 open community camp20160827 open community camp
20160827 open community campChia-Chi Chang
 
20160827 open community camp
20160827 open community camp20160827 open community camp
20160827 open community campChia-Chi Chang
 
Learning notes of r for python programmer (Temp1)
Learning notes of r for python programmer (Temp1)Learning notes of r for python programmer (Temp1)
Learning notes of r for python programmer (Temp1)Chia-Chi Chang
 
素食丙級考試流程重點整理
素食丙級考試流程重點整理素食丙級考試流程重點整理
素食丙級考試流程重點整理Chia-Chi Chang
 

Mais de Chia-Chi Chang (11)

Power BI x R
Power BI x RPower BI x R
Power BI x R
 
how to learn quantmod and quantstrat by yourself
how to learn quantmod and quantstrat by yourselfhow to learn quantmod and quantstrat by yourself
how to learn quantmod and quantstrat by yourself
 
Communicate with your data 20170104
Communicate with your data 20170104Communicate with your data 20170104
Communicate with your data 20170104
 
20161110 quantstrat in seattle
20161110 quantstrat in seattle20161110 quantstrat in seattle
20161110 quantstrat in seattle
 
ETL in R
ETL in RETL in R
ETL in R
 
PyData SF 2016 --- Moving forward through the darkness
PyData SF 2016 --- Moving forward through the darknessPyData SF 2016 --- Moving forward through the darkness
PyData SF 2016 --- Moving forward through the darkness
 
20160827 open community camp
20160827 open community camp20160827 open community camp
20160827 open community camp
 
20160827 open community camp
20160827 open community camp20160827 open community camp
20160827 open community camp
 
20130107 MLDM Monday
20130107 MLDM Monday20130107 MLDM Monday
20130107 MLDM Monday
 
Learning notes of r for python programmer (Temp1)
Learning notes of r for python programmer (Temp1)Learning notes of r for python programmer (Temp1)
Learning notes of r for python programmer (Temp1)
 
素食丙級考試流程重點整理
素食丙級考試流程重點整理素食丙級考試流程重點整理
素食丙級考試流程重點整理
 

20130325 mldm monday spide r