SlideShare a Scribd company logo
金融業菜鳥實習生的
python project 初體驗
2013/9/28
Yen @ PyLadies Meeting
Who am I ?

Yen ( 顏嘉儀 )
台大經濟所碩二
Python, R user
Why I am here ?
2

怎麼了嗎 (警戒= =+)

那來PyLadies分享
一下吧(遠目)
3

聽說..你暑假實習...
用python…寫了啥碗糕...是
吧....

1
Take my Internship experiment
as an Example
- Crawler What wil happen
if you were a Python programmer
in Financial Community ?
It’s
about
4
month
ago...

__ __ 人壽信託部
__ __ 人壽精算部
__ __ 券商自營部
Python 可以吃嗎?
你用py…(欸,這怎麼念阿) python 寫過
爬蟲阿, 可是我們都寫C# 跟VBA耶。
__ __ 券商自營部

不過沒關係,
上班前學會C#, VBA, SQL就OK了!

迷之聲:
有這麼容易嗎QQ...
三項任務
簡單來說就是:資料的蒐集、整理與分析
#1. 大連期貨商品研究
#2. 股票節稅 報價估計
#3. 每日期權strike, price戰力分佈圖
#1 大連商品交易所

根據 FIA統計,
2012 年全球期貨市場萎縮

15.3%
除了, 中國 & 印度

所謂期貨,是一種
合約,承諾在固定
期限內以一個特定
價格買入或賣出固
定數量的商品或金
融產品。
#1 大連商品交易所
#1 大連商品交易所 (R)

using R

549.5%
Tell me what happened?
#2 股票節稅報價估計 (VBA)
什麼是股票節稅?

summary: 平均來說,報價0.7%
沒超過500萬不太會做~
#3 每日期權strike, price戰力分佈圖

去年我們想做一件事,
但是沒完成...
Py ladies 0928
台指選擇權 201309到期 賣權

買方

#3 每日期權strike, price分佈圖

券商 1, 2, 3, 4

strike price 履約價格

賣方

券商 1, 2, 3, 4

於是主管畫了一張圖給我...
#3 每日期權strike, price戰力分佈圖
strike price 履約價格
反映對市場的預期?

所謂選擇權,是一種權
利契約,買方支付權利
金後,便有權利在未來
約定的某特定日期(到
期日),依約定之履約
價格(Strike Price),買
入或賣出一定數量的
約定標的物。
#3每日期權strike, price戰力分佈圖
籌碼分析: 反應對手成本?
#3 每日期權strike, price戰力分佈圖
期貨、選擇權也有買賣日報表
2012/7/2 -> 2012/7/3 : 成交量pooling :'(
Our Guess: 造市者
XX證券/法國興業證券/奧帝華證券/中信銀行

(there’s a graph but, for some reason, we skip it here )
簡單來說,就是抓資料麻~
We had done it in C# ,
but…
一次只能抓一頁 orz...

Target:可以一次抓很多頁就贏了 XD
簡單來說,就是抓資料嘛~

C#
[ C# ] Arachnode.net ? (not free)
R is slower
Try Python Solution:Scrapy

Windows Platform : C#
Scrapy
(official)
is a web crawling framework,
used to crawl websites and extract
structured data from their pages.
是一個可以讓你快速開發網路爬蟲的套件。
多快? 為什麼快?
Traditional Solution

connector

regular
expression

Request,
Response

parser

scrapy
Scrapy is well-structured framework

Parser

selfdefined

Connector
(Twisted)
XPath Parser
Scrapy, is a web crawling framework,
used to crawl websites and extract

structured data from their pages.
# Regular Expression
Every characters are
treated as the same
# Alternatives: XPath
html doc can be a
strudtured data
XPath is like “address”

# C://Python27
# html/body/div[@class="wrapper"]/
div[class="header.clearfix"]/h1[class="
logo"]/a
Chrome Plugin: XPath Helper
A Simple Demo
http://www.taifex.com.tw/chinese/index.
asp

1st Response

期交所首頁
Rule : XPath

url

url

2nd Request

url

url

url

公司簡介

商品

盤後資訊

…...
2nd Response

item

….

item

….

item

開盤價

item

Rule : XPath

….
import scrapy工具

Pseudo Code

class my_Spider( 繼承scrapy寫好的spider ):
name ="爬蟲名字"
start_urls=[initial request(URL)]
i.e. 最上層的root
def parse(self, first_response):
第一個parse函式寫死的!!
hxs = HtmlXPathSelector( first_response )
html_response => Xpath 結構化物件
Xpath = "爬取url的Rule"
extracted data = hxs.select(Xpath).extract()
yield Request(url=爬到的url, callback=self.next_parser)
def next_parser(self, second_response):
hxs = HtmlXPathSelector(second_response)
Xpath = "爬取資料的Rule"
extracted data = hxs.select(Xpath).extract()

函式名字隨便訂
cmd command

# 建立scrapy 專案
[cmd]scrapy startproject PyLadiesDemo

# 執行name = "TaiTex" 的爬蟲
[cmd]scrapy crawl TaiFex

請參考官方文件:http://doc.scrapy.org/en/latest/intro/tutorial.html
Demo
# sample code
# Good Tools: (error detect : try except)
[cmd] scrapy Shell url_that_u_want_2_crawl
例外處理 + pdb 下中斷點
# 例外處理
def parse(self, response):
try:
...
except:
網站難免有些例外
…

# 下中斷點
import pdb
pdb.set_trace()

or 弄錯XPath,
可以很即時的修正。
With Scrapy,

Everything
seems
easy and wonderful !

只要可以畫成線性節點圖
#3 每日期權strike, price戰力分佈圖
Find
the “url pattern”

crawl pages and
scrap data

store into DB

Visualization:
histogram

Google Developer Tools

Python
Scrapy
XPath Helper

Django -> MSSQL

Excel

Windows
Platform
事情沒有你想的容易...
之一
Step 1
Find the “url pattern”
Step 1
Find the “url pattern”

query string: http://www.taifex.com.tw/chinese/3/fcm_opt_rep.asp?
commodity_id=TXO&commodity_idt=TXO&settlemon=201310W1&pccode=P&curpage=1&pagenum=1
換月時點有許多例外

Step 1
Find the “url pattern”

[TXO] 3近2季
一般選擇權:每個月第3個禮拜三結算
週選擇權:每週星期三結算
what if 颱風假,連假?
[TGO] 6 近
偶數月份
事情沒有你想的容易...
之二
Step 2
crawl pages and
scrap data
什麼!! pip 不能用 !!!!!

Step 2
crawl pages and
scrap data

擋網站 !!!

因為公司

◢ ▆ ▅ ▄ ▃ 崩╰ (〒皿〒)╯ 潰▃ ▄ ▅ ▇ ◣
(installer using .bat file)
Python is beautiful ...
python crawler 49 行 (ver1)
150行 (final ver)
v.s.
C# crawler 700 行
(還只能爬一頁!!)

大勝!
事情沒有你想的容易...
之三
Step 3
store into DB
可以用MSSQL嗎?
Django 可以接M$SQL嗎?

YES !
< Solution > install “Django-mssql”
modify “settings.py”
http://django-mssql.readthedocs.org/en/latest/
如果可以重來一次的話
我會直接寫txt出來
再進MSSQL... = =a

因為
1. 不用跟主管解釋django是啥(?)
2. 快
3. 寫code測試
事情沒有你想的容易...
之四
Step 4
Visualization:
histogram
Py ladies 0928
好慢阿~
DB 膨脹的速度很快(200萬)
Excel 很慢,很慢,很~慢~~

<Solution>
分成history table, today table
事情沒有你想的容易...
之五
可以打包成exe檔嗎?
<Solution 1> Py2exe
改由python script啟動crawler

Fail ! @@
https://scrapy.readthedocs.org/en/latest/topics/practices.html
可以打包成exe檔嗎?
<Solution 2> Installer
.bat
use pip offline
(no update)
it works !
(while fails at a few
machines)
Conclusion
# scrapy :還不錯的輕量級crawler框架
快速開發,專注在parser上
簡單易學,好維護
Remember 例外處理
# django: 殺雞用牛刀了
若不考慮發展其他產品,
直接寫txt出來也許更省力
# 收穫:主管終於知道Python怎麼念了! <( ̄︶ ̄)>
what will happened ….

你可能會沒有

pip 可以用XD

正經的: C #, VBA
Reference
# official website tutorial
www.scrapy.org

# Taipei.py Thoen’s slide
http://files.meetup.com/6816242/%28Pycon%20Taipei%29%20Scrapy-20130328.pdf

# Thanks Tim & c3h3 !

More Related Content

PPT
Python 入门
kuco945
 
PDF
Python 于 webgame 的应用
勇浩 赖
 
PDF
[系列活動] Python 程式語言起步走
台灣資料科學年會
 
PDF
Python学习笔记
Lingfei Kong
 
PDF
Programming python - part 1
Che-Cheng Hsu
 
PPT
ios分享
Lucien Li
 
PDF
從 REPL 到 IDE
Justin Lin
 
PDF
Python crawling tutorial
Chen-Ming Yang
 
Python 入门
kuco945
 
Python 于 webgame 的应用
勇浩 赖
 
[系列活動] Python 程式語言起步走
台灣資料科學年會
 
Python学习笔记
Lingfei Kong
 
Programming python - part 1
Che-Cheng Hsu
 
ios分享
Lucien Li
 
從 REPL 到 IDE
Justin Lin
 
Python crawling tutorial
Chen-Ming Yang
 

What's hot (17)

PDF
型態與運算子
Justin Lin
 
PPT
页游开发中的 Python 组件与模式
勇浩 赖
 
PPTX
Python 爬蟲實戰
Chen-Ming Yang
 
PDF
论 Python 与设计模式。
勇浩 赖
 
PDF
C python 原始碼解析 投影片
kao kuo-tung
 
PDF
Pycontw2013x
weijr
 
PDF
[COSCUP2013] Python, F#, Golang and Friends
weijr
 
PDF
Python系列3
數真 蔡
 
PDF
Python 起步走
Justin Lin
 
PDF
[系列活動] Python爬蟲實戰
台灣資料科學年會
 
PDF
Python系列4
數真 蔡
 
PDF
Python匯出入csv以及繪製圖表初稿
jiannrong
 
PPT
Coreseek/Sphinx 全文检索实践指南
HonestQiao
 
PDF
[系列活動] Python 爬蟲實戰
台灣資料科學年會
 
PPT
iPhone,ios,Object-C基础入门
Lucien Li
 
PDF
《Python 3.5 技術手冊》第二章草稿
Justin Lin
 
PDF
open() 與 io 模組
Justin Lin
 
型態與運算子
Justin Lin
 
页游开发中的 Python 组件与模式
勇浩 赖
 
Python 爬蟲實戰
Chen-Ming Yang
 
论 Python 与设计模式。
勇浩 赖
 
C python 原始碼解析 投影片
kao kuo-tung
 
Pycontw2013x
weijr
 
[COSCUP2013] Python, F#, Golang and Friends
weijr
 
Python系列3
數真 蔡
 
Python 起步走
Justin Lin
 
[系列活動] Python爬蟲實戰
台灣資料科學年會
 
Python系列4
數真 蔡
 
Python匯出入csv以及繪製圖表初稿
jiannrong
 
Coreseek/Sphinx 全文检索实践指南
HonestQiao
 
[系列活動] Python 爬蟲實戰
台灣資料科學年會
 
iPhone,ios,Object-C基础入门
Lucien Li
 
《Python 3.5 技術手冊》第二章草稿
Justin Lin
 
open() 與 io 模組
Justin Lin
 
Ad

Similar to Py ladies 0928 (20)

PDF
getPDF.aspx
byron zhao
 
PDF
getPDF.aspx
byron zhao
 
PDF
A brief introduction to Python
bugway
 
PDF
手把手打開Python資料分析大門
Yen-lung Tsai
 
PPTX
Reactive application with akka.NET & .NET Core
Chen-Tien Tsai
 
PDF
Php More
henbo
 
PDF
Python速成指南
March Liu
 
PDF
Learning python in the motion picture industry by will zhou
Will Zhou
 
PPT
基于XMPP的Gtalk机器人
roamin9 Zhou
 
PPTX
DevOpsDays Taipei 2018 - Puppet 古早味、新感受:改造老牌企業進入自動化時代
scott liao
 
PDF
HITCON CTF 2014 BambooFox 解題心得分享
Chong-Kuan Chen
 
PPTX
Java annotation
Trinea Trinea
 
PDF
第一次程式設計就上手 - 使用Python 與周蟒(zhpy)
Fred Lin
 
PPT
Python Basic
雨蒼 林
 
PDF
用Cython封装c++代码为python模块的一点经验
Leo Zhou
 
PPTX
Python 爬蟲
Andy Yao
 
PPTX
Y3CDS - Python class 01
Ting-You Xu
 
PPTX
LineBot.pptx
NCUDSC
 
PDF
Introduction to Parse JavaScript SDK
維佋 唐
 
PDF
Python的module机制与最佳实践
Leo Zhou
 
getPDF.aspx
byron zhao
 
getPDF.aspx
byron zhao
 
A brief introduction to Python
bugway
 
手把手打開Python資料分析大門
Yen-lung Tsai
 
Reactive application with akka.NET & .NET Core
Chen-Tien Tsai
 
Php More
henbo
 
Python速成指南
March Liu
 
Learning python in the motion picture industry by will zhou
Will Zhou
 
基于XMPP的Gtalk机器人
roamin9 Zhou
 
DevOpsDays Taipei 2018 - Puppet 古早味、新感受:改造老牌企業進入自動化時代
scott liao
 
HITCON CTF 2014 BambooFox 解題心得分享
Chong-Kuan Chen
 
Java annotation
Trinea Trinea
 
第一次程式設計就上手 - 使用Python 與周蟒(zhpy)
Fred Lin
 
Python Basic
雨蒼 林
 
用Cython封装c++代码为python模块的一点经验
Leo Zhou
 
Python 爬蟲
Andy Yao
 
Y3CDS - Python class 01
Ting-You Xu
 
LineBot.pptx
NCUDSC
 
Introduction to Parse JavaScript SDK
維佋 唐
 
Python的module机制与最佳实践
Leo Zhou
 
Ad

Recently uploaded (20)

PPTX
一比一制作南卫理公会大学毕业证SSMU毕业证学校版本
g4jp77xf2
 
PPTX
一比一制作北卡罗来纳州立大学毕业证NCSU毕业证学校版本
tenqxr2ky
 
PPTX
一比一制作贝勒大学毕业证Baylor毕业证学校版本
srzw5w6jh
 
PPTX
新西兰学位证书,奥塔哥大学毕业证学历认证Otago毕业证加急购买
yw0wm7grzexpu69wx
 
PPTX
一比一制作墨尔本大学毕业证UniMelb毕业证学校版本
4vrmnd7m
 
PPTX
一比一制作爱荷华大学毕业证UI毕业证学校版本
g4jp77xf2
 
PPTX
class deco words for every class PAK 21.pptx
huiyentan1
 
PPTX
Chinese learning class lectures Lesson 16.pptx
SyedveedHde
 
PPTX
一比一制作北卡罗莱纳州立大学毕业证NC State毕业证学校版本
tenqxr2ky
 
PPTX
一比一制作拉筹伯大学毕业证LTU毕业证学校版本
4vrmnd7m
 
PPTX
一比一制作俄亥俄大学毕业证OU毕业证学校版本
tenqxr2ky
 
PPTX
一比一制作莫纳什大学毕业证Monash毕业证学校版本
4vrmnd7m
 
PPTX
新西兰学位证书,加州大学圣塔芭芭拉分校毕业证学历认证UCSB毕业证购买
yw0wm7grzexpu69wx
 
PPTX
一比一制作堪萨斯大学毕业证UKansas毕业证学校版本
tenqxr2ky
 
PPTX
一比一制作马凯特大学毕业证MU毕业证学校版本
srzw5w6jh
 
PPTX
美国学位证书,帝国理工学院毕业证学历认证Imperial College毕业证咨询
3of9
 
PPTX
活佛師尊: 「半部論語治天下, 移風易俗百孝經。」 鍾離大仙云: 「百孝經舉,孝傳塵世, 經歷千載不磨。」百孝經聖訓輯要第九.pptx
ray387615
 
PPTX
26每每风雨过后,特别是下雨过后,人们会感到较明显的降温。故有:“一场秋雨(风)一场寒”之说。
g4jp77xf2
 
PPTX
一比一制作印地安那大学伯明顿分校毕业证IUB毕业证学校版本
srzw5w6jh
 
PPTX
美国学位证书,奥本大学毕业证学历认证Auburn毕业证办
ewzt6ecilqqoketh
 
一比一制作南卫理公会大学毕业证SSMU毕业证学校版本
g4jp77xf2
 
一比一制作北卡罗来纳州立大学毕业证NCSU毕业证学校版本
tenqxr2ky
 
一比一制作贝勒大学毕业证Baylor毕业证学校版本
srzw5w6jh
 
新西兰学位证书,奥塔哥大学毕业证学历认证Otago毕业证加急购买
yw0wm7grzexpu69wx
 
一比一制作墨尔本大学毕业证UniMelb毕业证学校版本
4vrmnd7m
 
一比一制作爱荷华大学毕业证UI毕业证学校版本
g4jp77xf2
 
class deco words for every class PAK 21.pptx
huiyentan1
 
Chinese learning class lectures Lesson 16.pptx
SyedveedHde
 
一比一制作北卡罗莱纳州立大学毕业证NC State毕业证学校版本
tenqxr2ky
 
一比一制作拉筹伯大学毕业证LTU毕业证学校版本
4vrmnd7m
 
一比一制作俄亥俄大学毕业证OU毕业证学校版本
tenqxr2ky
 
一比一制作莫纳什大学毕业证Monash毕业证学校版本
4vrmnd7m
 
新西兰学位证书,加州大学圣塔芭芭拉分校毕业证学历认证UCSB毕业证购买
yw0wm7grzexpu69wx
 
一比一制作堪萨斯大学毕业证UKansas毕业证学校版本
tenqxr2ky
 
一比一制作马凯特大学毕业证MU毕业证学校版本
srzw5w6jh
 
美国学位证书,帝国理工学院毕业证学历认证Imperial College毕业证咨询
3of9
 
活佛師尊: 「半部論語治天下, 移風易俗百孝經。」 鍾離大仙云: 「百孝經舉,孝傳塵世, 經歷千載不磨。」百孝經聖訓輯要第九.pptx
ray387615
 
26每每风雨过后,特别是下雨过后,人们会感到较明显的降温。故有:“一场秋雨(风)一场寒”之说。
g4jp77xf2
 
一比一制作印地安那大学伯明顿分校毕业证IUB毕业证学校版本
srzw5w6jh
 
美国学位证书,奥本大学毕业证学历认证Auburn毕业证办
ewzt6ecilqqoketh
 

Py ladies 0928