Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
我的PYTHON爬蟲編年史
Search
bingroom
January 09, 2019
Programming
0
190
我的PYTHON爬蟲編年史
Invited talk in Python Hsinchu User Group
bingroom
January 09, 2019
Tweet
Share
More Decks by bingroom
See All by bingroom
我如何在PyCon找到乾爹 ─ 工人智慧的展現
bingroom
0
230
厲害了,我的蟲 - 我在iii的爬蟲人生
bingroom
0
500
Other Decks in Programming
See All in Programming
Node-RED を(HTTP で)つなげる MCP サーバーを作ってみた
highu
0
120
AIプログラマーDevinは PHPerの夢を見るか?
shinyasaita
1
230
Result型で“失敗”を型にするPHPコードの書き方
kajitack
5
940
PicoRuby on Rails
makicamel
2
140
なぜ適用するか、移行して理解するClean Architecture 〜構造を超えて設計を継承する〜 / Why Apply, Migrate and Understand Clean Architecture - Inherit Design Beyond Structure
seike460
PRO
3
780
『自分のデータだけ見せたい!』を叶える──Laravel × Casbin で複雑権限をスッキリ解きほぐす 25 分
akitotsukahara
2
640
フロントエンドのパフォーマンスチューニング
koukimiura
4
590
なぜ「共通化」を考え、失敗を繰り返すのか
rinchoku
1
660
20250704_教育事業におけるアジャイルなデータ基盤構築
hanon52_
5
850
Webの外へ飛び出せ NativePHPが切り拓くPHPの未来
takuyakatsusa
2
570
Advanced Micro Frontends: Multi Version/ Framework Scenarios @WAD 2025, Berlin
manfredsteyer
PRO
0
320
ISUCON研修おかわり会 講義スライド
arfes0e2b3c
1
450
Featured
See All Featured
Done Done
chrislema
184
16k
Why You Should Never Use an ORM
jnunemaker
PRO
58
9.4k
Reflections from 52 weeks, 52 projects
jeffersonlam
351
20k
Keith and Marios Guide to Fast Websites
keithpitt
411
22k
The Power of CSS Pseudo Elements
geoffreycrofte
77
5.9k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
32
2.4k
Bash Introduction
62gerente
613
210k
Build your cross-platform service in a week with App Engine
jlugia
231
18k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
656
60k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.4k
Producing Creativity
orderedlist
PRO
346
40k
[RailsConf 2023] Rails as a piece of cake
palkan
55
5.7k
Transcript
౯ጱPYTHON粖恝翥ଙݥ Bingroom
About Me • Data engineer in • Comprehending DevOps •
Addiction to and
None
C# PHP JAVA Python窕 أ㭏ၹ
Python窕 أ㭏ၹ
None
None
History Events
History Events Mechanize Selenium + Tkinter Web API PyQuery
Outline • Mechanize: web automation • Selenium (with Tkinter): build
a tool to load dynamic content • Web API: find it, use it • PyQuery: fast crawler prototyping
Challenge 1: PTT
The Soup Everyone Loves
The Soup Everyone Loves
The Soup Everyone Loves
import mechanize
import mechanize
None
None
None
None
None
None
None
import mechanize Only in Python2
Python3: mechanicalsoup
Missing part in mechanicalsoup https://findbiz.nat.gov.tw/fts/query/QueryBar/queryInit.do
Missing part in mechanicalsoup
Missing part in mechanicalsoup
Challenge 2: Facebook Personal IDs
Personal ID?
Personal ID: OK
How to query personal pages?
How to query personal pages? By Facebook ’s Vulnerability
How to deal with AJAX?
How to deal with AJAX? Selenium: for loading dynamic content
PhantomJS: headless browser
PhantomJS: headless browser Headless Chrome
PIL: crop reCAPTCHA
PhantomJS: headless browser Headless Chrome PIL: crop reCAPTCHA Tkinter: GUI
design
PhantomJS: headless browser Headless Chrome cx_Freeze: everything to executable PIL:
crop reCAPTCHA Tkinter: GUI design
Good Game
Challenge 3: 22 Government Official Sites
News & Attachment 岄玖૱(tpe)物https://health.gov.taipei/Default.aspx?tabid=36&mid=442 岄Ӿ૱(txg)物 http://www.health.taichung.gov.tw/26216/26204/26216/26204/26201/26204/27056/Lpsimplelist चᵇ૱(klu)物 http://www.klchb.gov.tw/ch/news/newspaper/list.aspx?c0=640 岄ܖ૱(tnn)物 https://health.tainan.gov.tw/list.asp?nsub=A0A600&topage=1
ṛᵜ૱(khh)物 http://khd.kcg.gov.tw/Main.aspx?sn=398 碝玖૱(ntpe)物 http://www.health.ntpc.gov.tw 葦翻(ilc)物 http://www.ilshb.gov.tw/index.php?catid=23&cid=4 ໘瑼૱(tyc)物 http://dph.tycg.gov.tw/home.jsp?id=13 ࡱ嬝૱(cyi)物 https://www.cichb.gov.tw/news/indexpda.asp 碝ᒓ翻(hsh)物 http://www.hcshb.gov.tw/home.jsp?mserno=200802220002&serno=200802220015&menudata=HcshbMenu& 碝ᒓ૱(hsc)物 http://www.hccg.gov.tw/MunicipalNews?websitedn=ou=hcchb,ou=ap_root,o=hccg,c=tw&language=chinese 舏礣翻(mal)物 https://www.mlshb.gov.tw/tc/PressRelease.aspx?pn=1&department=7 ܖಭ翻(nto)物 http://www.ntshb.gov.tw/Default.aspx 玕翻(cwh)物 http://www.chshb.gov.tw/news/?type_id=127&top=0 襇翻(yun)物 http://www.ylshb.gov.tw/news/index.php?m=9&m1=14&m2=35 ࡱ嬝翻(chy)物 https://cyshb.cyhg.gov.tw/News.aspx?n=E236E03D6AE796F8&sms=A55ECAF6D99EACF8 䩚翻(pch)物 http://www.ptshb.gov.tw/News.aspx?CategorySN=1894&n=CC774672B906BC5E 臺荳翻(hwa)物 http://www.hlshb.gov.tw/files/40-1006-27-1.php 岄䩚翻(ttt)物 http://www.ttshb.gov.tw/files/40-1000-12-1.php?Lang=zh-tw ᰂ槹翻(kmn)物 http://phb.kinmen.gov.tw/News.aspx?n=87FB8DD4C759A8B5&sms=A2C62D68901B977C ᄯ竝翻(peh)物 https://www.phchb.gov.tw/home.jsp?id=23 蝫翻(lnn)物 http://www.matsuhb.gov.tw/2009web/news/news_contents.php?room=news1
News & Attachment 岄玖૱(tpe)物https://health.gov.taipei/Default.aspx?tabid=36&mid=442 岄Ӿ૱(txg)物 http://www.health.taichung.gov.tw/26216/26204/26216/26204/26201/26204/27056/Lpsimplelist चᵇ૱(klu)物 http://www.klchb.gov.tw/ch/news/newspaper/list.aspx?c0=640 岄ܖ૱(tnn)物 https://health.tainan.gov.tw/list.asp?nsub=A0A600&topage=1
ṛᵜ૱(khh)物 http://khd.kcg.gov.tw/Main.aspx?sn=398 碝玖૱(ntpe)物 http://www.health.ntpc.gov.tw 葦翻(ilc)物 http://www.ilshb.gov.tw/index.php?catid=23&cid=4 ໘瑼૱(tyc)物 http://dph.tycg.gov.tw/home.jsp?id=13 ࡱ嬝૱(cyi)物 https://www.cichb.gov.tw/news/indexpda.asp 碝ᒓ翻(hsh)物 http://www.hcshb.gov.tw/home.jsp?mserno=200802220002&serno=200802220015&menudata=HcshbMenu& 碝ᒓ૱(hsc)物 http://www.hccg.gov.tw/MunicipalNews?websitedn=ou=hcchb,ou=ap_root,o=hccg,c=tw&language=chinese 舏礣翻(mal)物 https://www.mlshb.gov.tw/tc/PressRelease.aspx?pn=1&department=7 ܖಭ翻(nto)物 http://www.ntshb.gov.tw/Default.aspx 玕翻(cwh)物 http://www.chshb.gov.tw/news/?type_id=127&top=0 襇翻(yun)物 http://www.ylshb.gov.tw/news/index.php?m=9&m1=14&m2=35 ࡱ嬝翻(chy)物 https://cyshb.cyhg.gov.tw/News.aspx?n=E236E03D6AE796F8&sms=A55ECAF6D99EACF8 䩚翻(pch)物 http://www.ptshb.gov.tw/News.aspx?CategorySN=1894&n=CC774672B906BC5E 臺荳翻(hwa)物 http://www.hlshb.gov.tw/files/40-1006-27-1.php 岄䩚翻(ttt)物 http://www.ttshb.gov.tw/files/40-1000-12-1.php?Lang=zh-tw ᰂ槹翻(kmn)物 http://phb.kinmen.gov.tw/News.aspx?n=87FB8DD4C759A8B5&sms=A2C62D68901B977C ᄯ竝翻(peh)物 https://www.phchb.gov.tw/home.jsp?id=23 蝫翻(lnn)物 http://www.matsuhb.gov.tw/2009web/news/news_contents.php?room=news1
https://aji.tw/slides/pycon2017
https://aji.tw/slides/pycon2017
PyQuery == jQuery • parent element > child element •
# for id • . for class • . when a class name has space (i.e. has 2 classes)
None
#table_0 > tbody > tr:nth-child(1) > td.CCMS_jGridView_td_Class_1 Chrome Inspector
Firefox Inspector (Firebug) #table_0 > tbody:nth-child(2) > tr:nth-child(1) > td:nth-child(2)
None
Usage (1) Generate DOM object by URL opener
Usage (2) Traverse elements and list target items
Usage (2) Traverse elements and list target items
Usage (3) Laugh.
Usage (3) Laugh. #table_0 > tbody:nth-child(2) > tr:nth-child(1) > td:nth-child(2)
( ) td ጱ ᒫԫ㮆 ጱ text
Usage (3) Laugh. #table_0 > tbody:nth-child(2) > tr:nth-child(1) > td:nth-child(2)
( ) td ጱ ᒫԫ㮆 a ጱ 痀 href
Usage (3) Laugh.
Beautifulsoup V.S. PyQuery
Preparation for timeout issue
岄玖૱(tpe) 岄Ӿ૱(txg) चᵇ૱(klu) 岄ܖ૱(tnn) ṛᵜ૱(khh) 碝玖૱(ntpe) 葦翻(ilc) ໘瑼૱(tyc) ࡱ嬝૱(cyi) 碝ᒓ翻(hsh)
碝ᒓ૱(hsc) 舏礣翻(mal) ܖಭ翻(nto) 玕翻(cwh) 襇翻(yun) ࡱ嬝翻(chy) 䩚翻(pch) 臺荳翻(hwa) 岄䩚翻(ttt) ᰂ槹翻(kmn) ᄯ竝翻(peh) 蝫翻(lnn)
岄玖૱(tpe) 岄Ӿ૱(txg) चᵇ૱(klu) 岄ܖ૱(tnn) ṛᵜ૱(khh) 碝玖૱(ntpe) 葦翻(ilc) ໘瑼૱(tyc) ࡱ嬝૱(cyi) 碝ᒓ翻(hsh)
碝ᒓ૱(hsc) 舏礣翻(mal) ܖಭ翻(nto) 玕翻(cwh) 襇翻(yun) ࡱ嬝翻(chy) 䩚翻(pch) 臺荳翻(hwa) 岄䩚翻(ttt) ᰂ槹翻(kmn) ᄯ竝翻(peh) 蝫翻(lnn) Ӟ෭玖ṛ
䌃ਠᵍॠፗ矑硬粚 岄玖૱(tpe) 岄Ӿ૱(txg) चᵇ૱(klu) 岄ܖ૱(tnn) ṛᵜ૱(khh) 碝玖૱(ntpe) 葦翻(ilc) ໘瑼૱(tyc) ࡱ嬝૱(cyi)
碝ᒓ翻(hsh) 碝ᒓ૱(hsc) 舏礣翻(mal) ܖಭ翻(nto) 玕翻(cwh) 襇翻(yun) ࡱ嬝翻(chy) 䩚翻(pch) 臺荳翻(hwa) 岄䩚翻(ttt) ᰂ槹翻(kmn) ᄯ竝翻(peh) 蝫翻(lnn)
岄玖૱(tpe) 岄Ӿ૱(txg) चᵇ૱(klu) 岄ܖ૱(tnn) ṛᵜ૱(khh) 碝玖૱(ntpe) 葦翻(ilc) ໘瑼૱(tyc) ࡱ嬝૱(cyi) 碝ᒓ翻(hsh)
碝ᒓ૱(hsc) 舏礣翻(mal) ܖಭ翻(nto) 玕翻(cwh) 襇翻(yun) ࡱ嬝翻(chy) 䩚翻(pch) 臺荳翻(hwa) 岄䩚翻(ttt) ᰂ槹翻(kmn) ᄯ竝翻(peh) 蝫翻(lnn) encoding=Big5
岄玖૱(tpe) 岄Ӿ૱(txg) चᵇ૱(klu) 岄ܖ૱(tnn) ṛᵜ૱(khh) 碝玖૱(ntpe) 葦翻(ilc) ໘瑼૱(tyc) ࡱ嬝૱(cyi) 碝ᒓ翻(hsh)
碝ᒓ૱(hsc) 舏礣翻(mal) ܖಭ翻(nto) 玕翻(cwh) 襇翻(yun) ࡱ嬝翻(chy) 䩚翻(pch) 臺荳翻(hwa) 岄䩚翻(ttt) ᰂ槹翻(kmn) ᄯ竝翻(peh) 蝫翻(lnn) Date አ࿆㾴ଙ
岄玖૱(tpe) 岄Ӿ૱(txg) चᵇ૱(klu) 岄ܖ૱(tnn) ṛᵜ૱(khh) 碝玖૱(ntpe) 葦翻(ilc) ໘瑼૱(tyc) ࡱ嬝૱(cyi) 碝ᒓ翻(hsh)
碝ᒓ૱(hsc) 舏礣翻(mal) ܖಭ翻(nto) 玕翻(cwh) 襇翻(yun) ࡱ嬝翻(chy) 䩚翻(pch) 臺荳翻(hwa) 岄䩚翻(ttt) ᰂ槹翻(kmn) ᄯ竝翻(peh) 蝫翻(lnn) 猂硬አ࿆㾴ଙ
岄玖૱(tpe) 岄Ӿ૱(txg) चᵇ૱(klu) 岄ܖ૱(tnn) ṛᵜ૱(khh) 碝玖૱(ntpe) 葦翻(ilc) ໘瑼૱(tyc) ࡱ嬝૱(cyi) 碝ᒓ翻(hsh)
碝ᒓ૱(hsc) 舏礣翻(mal) ܖಭ翻(nto) 玕翻(cwh) 襇翻(yun) ࡱ嬝翻(chy) 䩚翻(pch) 臺荳翻(hwa) 岄䩚翻(ttt) ᰂ槹翻(kmn) ᄯ竝翻(peh) 蝫翻(lnn) ֦磪ݣ傀㰷独㻟牫 猂硬አ࿆㾴ଙ
岄玖૱(tpe) 岄Ӿ૱(txg) चᵇ૱(klu) 岄ܖ૱(tnn) ṛᵜ૱(khh) 碝玖૱(ntpe) 葦翻(ilc) ໘瑼૱(tyc) ࡱ嬝૱(cyi) 碝ᒓ翻(hsh)
碝ᒓ૱(hsc) 舏礣翻(mal) ܖಭ翻(nto) 玕翻(cwh) 襇翻(yun) ࡱ嬝翻(chy) 䩚翻(pch) 臺荳翻(hwa) 岄䩚翻(ttt) ᰂ槹翻(kmn) ᄯ竝翻(peh) 蝫翻(lnn) / 22 https://github.com/bingroom/city-health-news-TW
https://github.com/bingroom/city-health-news-TW 8 / 22 FAILED: 岄玖૱(tpe) 岄Ӿ૱(txg) चᵇ૱(klu) 岄ܖ૱(tnn) ṛᵜ૱(khh)
碝玖૱(ntpe) 葦翻(ilc) ໘瑼૱(tyc) ࡱ嬝૱(cyi) 碝ᒓ翻(hsh) 碝ᒓ૱(hsc) 舏礣翻(mal) ܖಭ翻(nto) 玕翻(cwh) 襇翻(yun) ࡱ嬝翻(chy) 䩚翻(pch) 臺荳翻(hwa) 岄䩚翻(ttt) ᰂ槹翻(kmn) ᄯ竝翻(peh) 蝫翻(lnn)
https://github.com/bingroom/city-health-news-TW 岄玖૱(tpe) 岄Ӿ૱(txg) चᵇ૱(klu) 岄ܖ૱(tnn) ṛᵜ૱(khh) 碝玖૱(ntpe) 葦翻(ilc) ໘瑼૱(tyc) ࡱ嬝૱(cyi)
碝ᒓ翻(hsh) 碝ᒓ૱(hsc) 舏礣翻(mal) ܖಭ翻(nto) 玕翻(cwh) 襇翻(yun) ࡱ嬝翻(chy) 䩚翻(pch) 臺荳翻(hwa) 岄䩚翻(ttt) ᰂ槹翻(kmn) ᄯ竝翻(peh) 蝫翻(lnn) 22 / 22 Successful:
Challenge 4: Hack Nielsen DAR System
How to get API from a website?
• Guess How to get API from a website?
How to get API from a website? • Guess
• Guess • Inspect How to get API from a
website?
• Guess • Inspect How to get API from a
website?
• Guess • Inspect How to get API from a
website?
Nielsen DAR Ad
Nielsen DAR Ad • Login by mechanize
Nielsen DAR Ad • Hold the header
Nielsen DAR Ad • Enjoy(?) the API
• Facebook • Gmail Inbox • Dcard • PChome Graph
API Multiprocess OAuth2.0 Xml base64 JSON regex random UA Keyword for Web APIs
Summary • Mechanize • BeautifulSoup • Selenium + Phantomjs •
APIs Less documentation Less limitation • Mechanicalsoup • PyQuery • Selenium + Chromedriver • APIs More documentation More limitation Past Now
None
PTT GOV websites Pchome Dcard Facebook Almost everything!
PTT GOV websites Pchome Dcard Facebook Almost everything!
Sikuli: PTT Crawler
Sikuli: Game Farming
Thanks for your listening! httpstatusdogs.com