Python 簡單教學
104522102 蔣佳峰
大
1. Python的基本介紹
2. 安裝直譯器與pycharm
3. 簡單的東西與程式碼
4. Crawler for PTT
基本介紹
Python是一種物件導向 直譯式的電腦程式語言
使用直譯器,執行 度較Java, C++慢
程式區塊使用縮排來界定範圍
應用範圍:Web程式,GUI開發,作業系統等
安裝直譯器與pycharm
請進入 頁 載直譯器 https://www.python.org/ 滑鼠移至Downloads 載2.7.12版
安裝直譯器與pycharm
環境變數設定:
Win7:系統內容->進階->環境變數->點擊path選擇編輯->在最 面新增c:\python27\ Win8&10:直接搜尋環境變數… 驟 Win7
安裝直譯器與pycharm
進入 頁:
https://www.jetbrains.com/pycharm/
點擊Download選擇Windows版,Community與Professsional 擇一 載 按照指示點擊完成安裝
Hello World !
• 單引號與雙引號在
python一樣都可 用來
表示字串
• print 面可 不用帶括
號
input&raw_input
input(): 接 輸入的內容並進行運算,若輸入的內容不是字串 數值 變數或
者boolean,則 成系統錯誤
raw_input(): 將輸入的內容一 視為字串
實際 的input是 樣:
def input(prompt):
return eval(raw_input(prompt))
簡單的程式碼
a=5
while(True):
pri t i put E ter:
B被認為是變數 稱
但是沒有被宣告而
成錯誤
List
List是一個可 被改變的有序性型態
假設s, t為兩List , x為一個變數,則可 使用 指 : x in s : x是否存在List s中
x not in s : x是否不存在List s中 s=s+t :將s與t連接起來存至List s中 len(s) :回傳List s的元素個數
s.append(x) :將x加入至List s的最 面
s.pop(i): 將List s中第i個元素去除,預設為第一個 s[-1]:List s最 一個元素
in 為 Python 的關鍵字
(keyword) 之一,用來 斷 複合資料型態 (compound data type) 之中否有某個元 素 (element) ,也就是在可 包含其他物件 (object) 的物 件之中 斷是否有某個物 件
簡單的程式碼
a=[1,2,3,4,5,6,7,8,9,10] print a
print a[0] print a[-1] print a[-2] print len(a) print a[0:9] print a[0:10:2] a.append(11) print a
a.pop(0) print a
簡單的程式碼
a=range(0,5) i=len(a)
print a
for x in range(5): print x,
while(i>0): print a[i-1] i=i-1
函數
def 函數 稱(參數1,參數 ,… :
…
return …
如果函式執行完畢但沒有使用return傳回值,則傳回None
簡單的程式碼
def bigger(a,b): if(a>b):
pri t a is bigger tha b ! elif(a<b):
print b is bigger than a ! else:
print The are the sa e !
Read & Write file
使用open(檔 ,讀 模式)
E : a=ope .t t , r context=a.read() 讀檔分為 種:
read:一次讀 整份文件 readline:一次只讀 一行
readlines:跟read類似,但是將整份文章內容一行行轉成list的一個元素 寫檔分為兩種
write
writelines:類似於readlines,將list裡的字串寫入檔案中
Read & Write file
with open('D:\\abc.txt','w') as ptr:
ptr.write('Oh ! I am so handsomeeeeeeeee !\n') ptr.write('It was just a dream !')
ptr.close()
with open('D:\\JohnCena.txt','r') as ptr2: while(True):
context=ptr2.readline() if(context==""):
break else:
print context, ptr.close()
Class
類 (class) 用來設計自己需要的物件 (object) ,類 是
物件的模板 Python 中設計類 使用關鍵字 class ,裡
頭可定義類 的類 屬性 (class attribute) 實體屬性
(instance attribute) 與方法 (method)
class vector():
def __init__(self,a=0,b=0): Constructor for initialization
self.a=a
self.b=b
def plus(self,y): method
self.a=self.a+y.a
self.b=self.b+y.b
def vector_print(self):method
print ('<%d,%d>')%(self.a,self.b)
x=vector()
x.vector_print() a=vector(5,4) a.vector_print() b=vector(5,6) a.plus(b)
a.vector_print()
urllib2
Urllib2用於開啟URLs,送出request
典型的應用包括從 頁獲 內容 自動化 頁爬蟲
開啟一個 頁
import urllib2
web=urllib2.urlopen('http://www.ncu.edu.tw/').read()
print type(web)
print web
BeautifulSoup
它可 拿來parsing html的內容,並擷 你想要的tag 及content資料 安裝 驟:
File->Settings->Project->Project Interpreter->install(green cross)->E ter bs ->install package 參考 站:
https://www.crummy.com/software/BeautifulSoup/bs4/doc/
import bs4
import urllib2
web=urllib2.urlopen('http://www.ncu.edu.tw/
').read()
print type(web)
result=bs4.BeautifulSoup(web,"html.parser")
print type(result)
print 'title:'+result.title.string
print 'link:'
for link in result.find_all('a'):
print type(link)
print(link.get('href'))
import bs4 import urllib2 import re
web=urllib2.urlopen(https://tw.news.yahoo.com/%E8%A8%B1%E5%A4%9A%E5%9C%8B%E5%A4% 96-pokemon-%E7%8E%A9%E5%AE%B6%E6%8A%B1%E6%80%A8-pokemon-
%E6%98%AF%E5%90%83%E6%B5%81%E9%87%8F%E6%80%AA%E7%8D%B8- 041600115.html').read()
p=re.compile(r'<.*?>')
result=bs4.BeautifulSoup(web,"html.parser") print 'title:'+result.title.string
for link in result.find_all('p'): context=str(link)
print p.sub('',context)
Regular Expression
def remove_html_tags(self, data): (把HTML Tag去除)
p = re.compile(r'<.*?>')
return p.sub('', data) (把regular expression找到的東西全部去掉)
需要import re
re.compile(pattern): 建立規則用來辨識長得像pattern的東西
例如:
re.compile ab+ 用來辨識ab,abb,abbb…
爬蟲(Crawler)
是一種利用HTTP Request 抓 路資料的技術
能自動化瀏覽 路的程式,有效率 得/更新 站內容
常利用 頁結構達到大規模收集頁面資料
Crawler PTT版
實作方式乃利用PTT 頁板文章顯示與頁面的結構化,達到簡單抓 文章的目的
對於任何看板的主頁面,其 址長得像 樣:
https://www.ptt.cc/bbs/看板 稱/index總頁數.html Ex: https://www.ptt.cc/bbs/Gossiping/index100.html
對於文章內容則是長成 樣:
https://www.ptt.cc/bbs/看板 稱/文章ID.html
Ex: https://www.ptt.cc/bbs/Gossiping/M.1119233779.A.191.html
陽春的PTT Crawler
https://github.com/paulyang0125/bbs_crawler_utility
讀 看板文章列表
if (self.useHeader):
request = urllib2.Request(page_url,headers=headers)
page = bs4.BeautifulSoup(urllib2.urlopen(request).read())
else:
page = bs4.BeautifulSoup(urllib2.urlopen(page_url).read())
對於頁面每一篇文章都進行一次走訪
for link in page.find_all(class_= r-ent ):
post_id = link.a.get( href ).split( / )[-1][:-5] if (self.useHeader):
request = urllib2.Request(post_url(post_id), headers=headers) post = bs4.BeautifulSoup(urllib2.urlopen(request).read())
else:
post = bs4.BeautifulSoup(urllib2.urlopen(post_url(post_id)).read()) with open(post_id+ .txt , w ) as contentFile_fp:
contentFile_fp.write(
Title: + post.title.string.encode( utf-8 ) + \n + \n )
contentFile_fp.write(self.remove_html_tags(str(post.find(id= main-container )) contentFile_fp.close()