丘兆欽
Web Scraping
入門篇 1
103/5/18
 Python 簡介
 Ez 擷取
 網站基本架構
 篩選過濾(初級)
Python 簡介
• 可讀性
• 社群與資源
• 標準函式庫
Python 簡介
• 宣告變數
• 呼叫方法
• 內建函式
words = ‘Hello, world!’
print(words.upper( )) # 印出 words 字串的大寫
=> HELLO, WORLD!
Ez 擷取
• pyquery (最平均)
• lxml (效率最高)
• twisted (牛刀)
• beautifulsoup (Ez use)
• urllib (內建)
Ez 擷取
# Python3
import urllib.request
url = ‘http://www.cwb.gov.tw/V7/index.htm’ #中央氣象局
data = urllib.request.urlopen(url)
htmltext = data.read( ).decode(‘utf-8’) # 擷取並存入 htmltext
print(htmltext)
data.close( )
Ez 擷取
網站基本架構
<html>
</html>
<head>
<title> </title>
</head>
<body>
<div> </div>
</body>
網站基本架構
• 開始 / 結束
• 連結
• 巢狀結構
• 其他函式
• class / id
<meta name="DC.Type" content="服務" />
<ul class="LifeWeather">
<h3 class="menu-icon01">生活氣象</h3>
<li>
<a href="/V7/life/Life.htm">生活氣象</a>
</li>
</ul>
<h3 class="menu-icon02">預報</h3>
篩選過濾(初級)
• 迴圈 while
• 判斷式 if
• 輸出函式 print()
• 內建函式 str.find()
Show time
篩選過濾(初級)

Web scraping入門1