01 python正則表達式python
02 Python Web編程web
03 Python多線程編程正則表達式
04 Python網絡編程數據庫
05 Python數據庫編程編程
-------------------------------------json
對字符串的匹配和檢索,經過re模塊提供對正則表達式的支持。cookie
. 匹配任意換行符之外的字符網絡
1 #python 3.6 2 import re 3 word = 'https://www.ichunqiu.com python_1.1' 4 key = re.findall('h.',word) 5 print (key)
結果是['ht', 'hu', 'ho']
\ 轉義字符多線程
#python 3.6 import re word = 'https://www.ichunqiu.com python_1.1' key = re.findall('\.',word) print (key)
['.', '.', '.']
[...]字符集。對應的位置能夠是字符集中任意字符,能夠逐個列出也能夠給出範圍,如[abc]或[a-c]。第一個字符是^則表明取反,[^abc]表明不是abc的其餘字符app
預約義字符集
\d 數字[0-9] a\dc a1c
\D 非數字[^\d] a\Dc abc
\s 空白字符 a\sc a c
\S 非空白字符 abc
\w 單詞字符[a-z A-Z 0-9] abc
\W 非單詞字符 a c
* 匹配前一個字符0次或無限次
+ 匹配前一個字符1次或無限次
? 匹配前一個字符0次或1次
{m} 匹配前一個字符m次
{m,n} 匹配前一個字符m-n次
| 左右表達式任意匹配一個
(..) 一個分組
Python裏的數量詞默認是貪婪的,老是嘗試匹配儘量多的字符;非貪婪的則相反,老是嘗試匹配儘量少的字符。
例如正則表達式ab*若是用於查找abbbc將找到abbbb,若是是非貪婪的ab*?,將找到a。
查找課程
http = ...
title = re.findall(r'title="(.*?)" onclick',http) for i in title: print (i)
關鍵詞:urllib/urllib/requests、爬蟲開發
1 import urllib,urllib2 2 url = 'http://www.baidu.com' 3 r = urllib.urlopen(url) //發送請求 4 print r.read() //接受回顯
urllib.urlretreve(url,fliename = None,reporthook=None,data=None) //下載文件
urllib2.Requests() //控制請求頭
urllib.urlretrieve('地址「',filename='E:\\google.png') //下載圖片
requests
發送網絡請求 requests.get(url) requests.post(url) requests.head(url)
爲URL傳遞參數payload={'key1':'value1';'key2','valu2'} r = requests.get(url,params=payload) //r.url=xxxx/get?key2=value2
響應內容 r.text r.content
定製請求頭 headers = {'content-type':'application/json'}
複雜的POST請求payload={'key1':'value1';'key2','valu2'} r = requests.post(url,data=payload)
狀態碼 r.status_code
響應頭 r.headers
Cookie r.cookies
請求超時 r = requests.get(url,timeout = 0.1)
網絡爬蟲
頁碼由字符串處理,加入headers頭,能夠用BP抓出來
r=request.get(url=url,headers=headers)
進程是程序的一次執行,每一個進程都有本身的地址空間、內存、數據棧及其餘記錄其運行軌跡的輔助數據。
全部的線程運行在同一個進程當中,共享相同的運行環境。線程有開始順序執行和結束三個部分。
start_new_thread(function,args kwargs=None)
1 import thread 2 import time 3 def fun1(): 4 print 'Hello world!%s'%time.ctime() 5 def main(): 6 thread.start_new_thread(fun1,()) 7 thread.start_new_thread(fun1,()) 8 time.sleep(2) 9 if __name__ == '__main__': 10 main()
//一個簡單的例子
1 //探測C段存活主機 2 #coding = utf-8 3 #ping 4 import thread 5 import time 6 from subprocess import Popen,PIPE 7 8 def ping_check(ip): 9 check = Popen(['ping.exe',ip],stdin=PIPE,stdout=PIPE) 10 data = check.stdout.read() 11 if 'TTL' in data: 12 print '[OK] %s'%ip 13 def main(): 14 for i in range(1,255): 15 ip = '111.47.226.'+str(i) 16 thread.start_new_thread(ping_check,(ip,)) 17 time.sleep(0.1) 18 if __name__ == '__main__': 19 main()
threading模塊
1 import threading 2 import time 3 4 def fun1(key): 5 print 'hello %s:%s'%(key,time.ctime()) 6 def main(): 7 threads=[] 8 keys=['danny','nico','nick','pipe'] 9 threads_count = len(keys) //定義線程數 10 for i in range(threads_count): 11 t = threading.Thread(target = fun1,args=(keys[i],)) 12 threads.append(t) 13 for i in range(threads_count): 14 threads[i].start() 15 for i in range(threads_count): 16 threads[i].join() 17 if __name__ == '__main__': 18 main()
生產者-消費者問題和Queue模塊
Queue模塊 qsize() empty() full() put() get()