SlideShare una empresa de Scribd logo
1 de 51
Descargar para leer sin conexión
金融業菜鳥實習生的
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戰力分佈圖

去年我們想做一件事,
但是沒完成...
台指選擇權 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
好慢阿~
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 !

Más contenido relacionado

La actualidad más candente

型態與運算子
型態與運算子型態與運算子
型態與運算子Justin Lin
 
页游开发中的 Python 组件与模式
页游开发中的 Python 组件与模式页游开发中的 Python 组件与模式
页游开发中的 Python 组件与模式勇浩 赖
 
论 Python 与设计模式。
论 Python 与设计模式。论 Python 与设计模式。
论 Python 与设计模式。勇浩 赖
 
C python 原始碼解析 投影片
C python 原始碼解析 投影片C python 原始碼解析 投影片
C python 原始碼解析 投影片kao kuo-tung
 
Pycontw2013x
Pycontw2013xPycontw2013x
Pycontw2013xweijr
 
[COSCUP2013] Python, F#, Golang and Friends
[COSCUP2013] Python, F#, Golang and Friends[COSCUP2013] Python, F#, Golang and Friends
[COSCUP2013] Python, F#, Golang and Friendsweijr
 
Python 起步走
Python 起步走Python 起步走
Python 起步走Justin Lin
 
Python匯出入csv以及繪製圖表初稿
Python匯出入csv以及繪製圖表初稿Python匯出入csv以及繪製圖表初稿
Python匯出入csv以及繪製圖表初稿jiannrong
 
Coreseek/Sphinx 全文检索实践指南
Coreseek/Sphinx 全文检索实践指南Coreseek/Sphinx 全文检索实践指南
Coreseek/Sphinx 全文检索实践指南HonestQiao
 
iPhone,ios,Object-C基础入门
iPhone,ios,Object-C基础入门iPhone,ios,Object-C基础入门
iPhone,ios,Object-C基础入门Lucien Li
 
《Python 3.5 技術手冊》第二章草稿
《Python 3.5 技術手冊》第二章草稿《Python 3.5 技術手冊》第二章草稿
《Python 3.5 技術手冊》第二章草稿Justin Lin
 
open() 與 io 模組
open() 與 io 模組open() 與 io 模組
open() 與 io 模組Justin Lin
 

La actualidad más candente (17)

型態與運算子
型態與運算子型態與運算子
型態與運算子
 
页游开发中的 Python 组件与模式
页游开发中的 Python 组件与模式页游开发中的 Python 组件与模式
页游开发中的 Python 组件与模式
 
Python 爬蟲實戰
Python 爬蟲實戰Python 爬蟲實戰
Python 爬蟲實戰
 
论 Python 与设计模式。
论 Python 与设计模式。论 Python 与设计模式。
论 Python 与设计模式。
 
C python 原始碼解析 投影片
C python 原始碼解析 投影片C python 原始碼解析 投影片
C python 原始碼解析 投影片
 
Pycontw2013x
Pycontw2013xPycontw2013x
Pycontw2013x
 
[COSCUP2013] Python, F#, Golang and Friends
[COSCUP2013] Python, F#, Golang and Friends[COSCUP2013] Python, F#, Golang and Friends
[COSCUP2013] Python, F#, Golang and Friends
 
Python系列3
Python系列3Python系列3
Python系列3
 
Python 起步走
Python 起步走Python 起步走
Python 起步走
 
[系列活動] Python爬蟲實戰
[系列活動] Python爬蟲實戰[系列活動] Python爬蟲實戰
[系列活動] Python爬蟲實戰
 
Python系列4
Python系列4Python系列4
Python系列4
 
Python匯出入csv以及繪製圖表初稿
Python匯出入csv以及繪製圖表初稿Python匯出入csv以及繪製圖表初稿
Python匯出入csv以及繪製圖表初稿
 
Coreseek/Sphinx 全文检索实践指南
Coreseek/Sphinx 全文检索实践指南Coreseek/Sphinx 全文检索实践指南
Coreseek/Sphinx 全文检索实践指南
 
[系列活動] Python 爬蟲實戰
[系列活動] Python 爬蟲實戰[系列活動] Python 爬蟲實戰
[系列活動] Python 爬蟲實戰
 
iPhone,ios,Object-C基础入门
iPhone,ios,Object-C基础入门iPhone,ios,Object-C基础入门
iPhone,ios,Object-C基础入门
 
《Python 3.5 技術手冊》第二章草稿
《Python 3.5 技術手冊》第二章草稿《Python 3.5 技術手冊》第二章草稿
《Python 3.5 技術手冊》第二章草稿
 
open() 與 io 模組
open() 與 io 模組open() 與 io 模組
open() 與 io 模組
 

Similar a Py ladies 0928

A brief introduction to Python
A brief introduction to PythonA brief introduction to Python
A brief introduction to Pythonbugway
 
手把手打開Python資料分析大門
手把手打開Python資料分析大門手把手打開Python資料分析大門
手把手打開Python資料分析大門Yen-lung Tsai
 
Reactive application with akka.NET & .NET Core
Reactive application with akka.NET & .NET CoreReactive application with akka.NET & .NET Core
Reactive application with akka.NET & .NET CoreChen-Tien Tsai
 
Php More
Php MorePhp More
Php Morehenbo
 
Python速成指南
Python速成指南Python速成指南
Python速成指南March Liu
 
Learning python in the motion picture industry by will zhou
Learning python in the motion picture industry   by will zhouLearning python in the motion picture industry   by will zhou
Learning python in the motion picture industry by will zhouWill Zhou
 
基于XMPP的Gtalk机器人
基于XMPP的Gtalk机器人基于XMPP的Gtalk机器人
基于XMPP的Gtalk机器人roamin9 Zhou
 
DevOpsDays Taipei 2018 - Puppet 古早味、新感受:改造老牌企業進入自動化時代
DevOpsDays Taipei 2018 - Puppet 古早味、新感受:改造老牌企業進入自動化時代DevOpsDays Taipei 2018 - Puppet 古早味、新感受:改造老牌企業進入自動化時代
DevOpsDays Taipei 2018 - Puppet 古早味、新感受:改造老牌企業進入自動化時代scott liao
 
HITCON CTF 2014 BambooFox 解題心得分享
HITCON CTF 2014 BambooFox 解題心得分享HITCON CTF 2014 BambooFox 解題心得分享
HITCON CTF 2014 BambooFox 解題心得分享Chong-Kuan Chen
 
第一次程式設計就上手 - 使用Python 與周蟒(zhpy)
第一次程式設計就上手  - 使用Python 與周蟒(zhpy)第一次程式設計就上手  - 使用Python 與周蟒(zhpy)
第一次程式設計就上手 - 使用Python 與周蟒(zhpy)Fred Lin
 
用Cython封装c++代码为python模块的一点经验
用Cython封装c++代码为python模块的一点经验用Cython封装c++代码为python模块的一点经验
用Cython封装c++代码为python模块的一点经验Leo Zhou
 
Python 爬蟲
Python 爬蟲 Python 爬蟲
Python 爬蟲 Andy Yao
 
Y3CDS - Python class 01
Y3CDS - Python class 01 Y3CDS - Python class 01
Y3CDS - Python class 01 Ting-You Xu
 
LineBot.pptx
LineBot.pptxLineBot.pptx
LineBot.pptxNCUDSC
 
Introduction to Parse JavaScript SDK
Introduction to Parse JavaScript SDKIntroduction to Parse JavaScript SDK
Introduction to Parse JavaScript SDK維佋 唐
 

Similar a Py ladies 0928 (20)

getPDF.aspx
getPDF.aspxgetPDF.aspx
getPDF.aspx
 
getPDF.aspx
getPDF.aspxgetPDF.aspx
getPDF.aspx
 
A brief introduction to Python
A brief introduction to PythonA brief introduction to Python
A brief introduction to Python
 
手把手打開Python資料分析大門
手把手打開Python資料分析大門手把手打開Python資料分析大門
手把手打開Python資料分析大門
 
Reactive application with akka.NET & .NET Core
Reactive application with akka.NET & .NET CoreReactive application with akka.NET & .NET Core
Reactive application with akka.NET & .NET Core
 
Php More
Php MorePhp More
Php More
 
Python速成指南
Python速成指南Python速成指南
Python速成指南
 
Learning python in the motion picture industry by will zhou
Learning python in the motion picture industry   by will zhouLearning python in the motion picture industry   by will zhou
Learning python in the motion picture industry by will zhou
 
基于XMPP的Gtalk机器人
基于XMPP的Gtalk机器人基于XMPP的Gtalk机器人
基于XMPP的Gtalk机器人
 
DevOpsDays Taipei 2018 - Puppet 古早味、新感受:改造老牌企業進入自動化時代
DevOpsDays Taipei 2018 - Puppet 古早味、新感受:改造老牌企業進入自動化時代DevOpsDays Taipei 2018 - Puppet 古早味、新感受:改造老牌企業進入自動化時代
DevOpsDays Taipei 2018 - Puppet 古早味、新感受:改造老牌企業進入自動化時代
 
HITCON CTF 2014 BambooFox 解題心得分享
HITCON CTF 2014 BambooFox 解題心得分享HITCON CTF 2014 BambooFox 解題心得分享
HITCON CTF 2014 BambooFox 解題心得分享
 
Java annotation
Java annotationJava annotation
Java annotation
 
第一次程式設計就上手 - 使用Python 與周蟒(zhpy)
第一次程式設計就上手  - 使用Python 與周蟒(zhpy)第一次程式設計就上手  - 使用Python 與周蟒(zhpy)
第一次程式設計就上手 - 使用Python 與周蟒(zhpy)
 
Python Basic
Python  BasicPython  Basic
Python Basic
 
用Cython封装c++代码为python模块的一点经验
用Cython封装c++代码为python模块的一点经验用Cython封装c++代码为python模块的一点经验
用Cython封装c++代码为python模块的一点经验
 
Python 爬蟲
Python 爬蟲 Python 爬蟲
Python 爬蟲
 
Python 2-基本語法
Python 2-基本語法Python 2-基本語法
Python 2-基本語法
 
Y3CDS - Python class 01
Y3CDS - Python class 01 Y3CDS - Python class 01
Y3CDS - Python class 01
 
LineBot.pptx
LineBot.pptxLineBot.pptx
LineBot.pptx
 
Introduction to Parse JavaScript SDK
Introduction to Parse JavaScript SDKIntroduction to Parse JavaScript SDK
Introduction to Parse JavaScript SDK
 

Último

EDUC6506_ClassPresentation_TC330277 (1).pptx
EDUC6506_ClassPresentation_TC330277 (1).pptxEDUC6506_ClassPresentation_TC330277 (1).pptx
EDUC6506_ClassPresentation_TC330277 (1).pptxmekosin001123
 
educ6506presentationtc3302771-240427173057-06a46de5.pptx
educ6506presentationtc3302771-240427173057-06a46de5.pptxeduc6506presentationtc3302771-240427173057-06a46de5.pptx
educ6506presentationtc3302771-240427173057-06a46de5.pptxmekosin001123
 
1.🎉“入侵大学入学考试中心修改成绩”来袭!ALEVEL替考大揭秘,轻松搞定考试成绩! 💥你还在为无法进入大学招生系统而烦恼吗?想知道如何通过技术手段更改...
1.🎉“入侵大学入学考试中心修改成绩”来袭!ALEVEL替考大揭秘,轻松搞定考试成绩! 💥你还在为无法进入大学招生系统而烦恼吗?想知道如何通过技术手段更改...1.🎉“入侵大学入学考试中心修改成绩”来袭!ALEVEL替考大揭秘,轻松搞定考试成绩! 💥你还在为无法进入大学招生系统而烦恼吗?想知道如何通过技术手段更改...
1.🎉“入侵大学入学考试中心修改成绩”来袭!ALEVEL替考大揭秘,轻松搞定考试成绩! 💥你还在为无法进入大学招生系统而烦恼吗?想知道如何通过技术手段更改...黑客 接单【TG/微信qoqoqdqd】
 
泽兰应用科学大学毕业证制作/定制国外大学录取通知书/购买一个假的建国科技大学硕士学位证书
泽兰应用科学大学毕业证制作/定制国外大学录取通知书/购买一个假的建国科技大学硕士学位证书泽兰应用科学大学毕业证制作/定制国外大学录取通知书/购买一个假的建国科技大学硕士学位证书
泽兰应用科学大学毕业证制作/定制国外大学录取通知书/购买一个假的建国科技大学硕士学位证书jakepaige317
 
哪里可以购买日本筑波学院大学学位记/做个假的文凭可认证吗/仿制日本大学毕业证/意大利语CELI证书定制
哪里可以购买日本筑波学院大学学位记/做个假的文凭可认证吗/仿制日本大学毕业证/意大利语CELI证书定制哪里可以购买日本筑波学院大学学位记/做个假的文凭可认证吗/仿制日本大学毕业证/意大利语CELI证书定制
哪里可以购买日本筑波学院大学学位记/做个假的文凭可认证吗/仿制日本大学毕业证/意大利语CELI证书定制jakepaige317
 
EDUC6506(001)_ClassPresentation_2_TC330277 (1).pptx
EDUC6506(001)_ClassPresentation_2_TC330277 (1).pptxEDUC6506(001)_ClassPresentation_2_TC330277 (1).pptx
EDUC6506(001)_ClassPresentation_2_TC330277 (1).pptxmekosin001123
 

Último (6)

EDUC6506_ClassPresentation_TC330277 (1).pptx
EDUC6506_ClassPresentation_TC330277 (1).pptxEDUC6506_ClassPresentation_TC330277 (1).pptx
EDUC6506_ClassPresentation_TC330277 (1).pptx
 
educ6506presentationtc3302771-240427173057-06a46de5.pptx
educ6506presentationtc3302771-240427173057-06a46de5.pptxeduc6506presentationtc3302771-240427173057-06a46de5.pptx
educ6506presentationtc3302771-240427173057-06a46de5.pptx
 
1.🎉“入侵大学入学考试中心修改成绩”来袭!ALEVEL替考大揭秘,轻松搞定考试成绩! 💥你还在为无法进入大学招生系统而烦恼吗?想知道如何通过技术手段更改...
1.🎉“入侵大学入学考试中心修改成绩”来袭!ALEVEL替考大揭秘,轻松搞定考试成绩! 💥你还在为无法进入大学招生系统而烦恼吗?想知道如何通过技术手段更改...1.🎉“入侵大学入学考试中心修改成绩”来袭!ALEVEL替考大揭秘,轻松搞定考试成绩! 💥你还在为无法进入大学招生系统而烦恼吗?想知道如何通过技术手段更改...
1.🎉“入侵大学入学考试中心修改成绩”来袭!ALEVEL替考大揭秘,轻松搞定考试成绩! 💥你还在为无法进入大学招生系统而烦恼吗?想知道如何通过技术手段更改...
 
泽兰应用科学大学毕业证制作/定制国外大学录取通知书/购买一个假的建国科技大学硕士学位证书
泽兰应用科学大学毕业证制作/定制国外大学录取通知书/购买一个假的建国科技大学硕士学位证书泽兰应用科学大学毕业证制作/定制国外大学录取通知书/购买一个假的建国科技大学硕士学位证书
泽兰应用科学大学毕业证制作/定制国外大学录取通知书/购买一个假的建国科技大学硕士学位证书
 
哪里可以购买日本筑波学院大学学位记/做个假的文凭可认证吗/仿制日本大学毕业证/意大利语CELI证书定制
哪里可以购买日本筑波学院大学学位记/做个假的文凭可认证吗/仿制日本大学毕业证/意大利语CELI证书定制哪里可以购买日本筑波学院大学学位记/做个假的文凭可认证吗/仿制日本大学毕业证/意大利语CELI证书定制
哪里可以购买日本筑波学院大学学位记/做个假的文凭可认证吗/仿制日本大学毕业证/意大利语CELI证书定制
 
EDUC6506(001)_ClassPresentation_2_TC330277 (1).pptx
EDUC6506(001)_ClassPresentation_2_TC330277 (1).pptxEDUC6506(001)_ClassPresentation_2_TC330277 (1).pptx
EDUC6506(001)_ClassPresentation_2_TC330277 (1).pptx
 

Py ladies 0928