正則表達(dá)式符號(hào)與方法
符號(hào).是一個(gè)占位符
import re
# .的使用舉例
a = 'xy123'
b = re.findall('x.',a)
print b
輸出:xy
符號(hào)*匹配前一個(gè)字符0次或無限次
#*的使用舉例
a = 'xyxy123'
b = re.findall('x*',a)
print b
輸出:['x', '', 'x', '', '', '', '', '']
符號(hào)?匹配前1個(gè)字符0次或1次
#?的使用舉例
a = 'xy123'
b = re.findall('x?',a)
print b
輸出:['x', '', '', '', '', '']
.:貪心算法
.?:非貪心算法
# .*的使用舉例
secret_code = 'hadkfalifexx|xxfasdjifja134xxlovexx23345sdfxxyouxx8dfse'
b = re.findall('xx.*xx',secret_code)
print b
輸出:['xx|xxfasdjifja134xxlovexx23345sdfxxyouxx']
# .*?的使用舉例
c = re.findall('xx.*?xx',secret_code)
print c
輸出:['xx|xx', 'xxlovexx', 'xxyouxx']
# 使用括號(hào)與不使用括號(hào)的差別
# 返回()內(nèi)的數(shù)據(jù)
d = re.findall('xx(.*?)xx',secret_code)
print d
for each in d:
print each
?輸出:['|', 'love', 'you']
|
love
you
. 無法處理換行符
s = '''sdfxxhello
xxfsdfxxworldxxasdf'''
d = re.findall('xx(.*?)xx',s)
print d
輸出:['fsdf']
# re.S的作用使 . 包括\n
s = '''sdfxxhello
xxfsdfxxworldxxasdf'''
d = re.findall('xx(.*?)xx',s,re.S)
print d
輸出:['hello\n', 'world']
對(duì)比findall與search的區(qū)別
s2 = 'asdfxxIxx123xxlovexxdfd'
f = re.search('xx(.*?)xx123xx(.*?)xx',s2).group(1) #輸出:I
# f = re.search('xx(.*?)xx123xx(.*?)xx',s2).group(2) #輸出:love
print f
#finall
f2 = re.findall('xx(.*?)xx123xx(.*?)xx',s2)
print f2[0][1] #輸出love
sub使用舉例:
實(shí)戰(zhàn)——制作文本爬蟲
目標(biāo)網(wǎng)站右鍵打不開戳气,換一個(gè)http://wiki.jikexueyuan.com/
找到圖片,右鍵檢查
在頁面源代碼搜索class="imgbox"
選擇這一部分,源代碼復(fù)制到source.txt
picdownloader.py
#!/usr/bin/python
#-*- coding:utf-8 -*-
import re
import requests
#讀取源代碼文件
f = open('source.txt','r')
html = f.read()
f.close()
#匹配圖片網(wǎng)址
pic_url = re.findall('img src="(.*?)" alt=',html,re.S)
i = 0
for each in pic_url:
print 'now downloading : ' + each
pic = requests.get(each)
fp = open('pic\\' + str(i) + '.jpg','wb')
fp.write(pic.content)
fp.close()
i += 1
手工找到頁面再下載顯得有點(diǎn)low:修改代碼使?其自動(dòng)獲取源代碼
#!/usr/bin/python
#-- coding:utf-8 --
import re
import requests
import urllib2
#讀取源代碼文件
#f = open('source.txt','r')
url = 'http://wiki.jikexueyuan.com/'
response = urllib2.urlopen(url)
htmls = response.read()
#匹配圖片網(wǎng)址
#由于是下載整個(gè)頁面,所以原來的正則表達(dá)式會(huì)匹配更多的內(nèi)容
#所以采用先抓大再抓小顽决,把范圍縮小~
picsurl = re.findall('<div class="imgbox">(.*?) </div>',htmls,re.S)
#findall返回的是一個(gè)列表,需要將列表再次弄成一個(gè)string,逐個(gè)寫入一個(gè)txt
f = open('url.txt','wb')
for i in picsurl:
f.write(i)
f.close()
ff = open('url.txt','r')
html = ff.read()
ff.close()
#這代碼寫得確實(shí)有點(diǎn)爛短条。。才菠。茸时。
pic_url = re.findall('img src="(.*?)" alt=',html,re.S)
i = 0
for each in pic_url:
print 'now downloading : ' + each
pic = requests.get(each)
fp = open('pic\\' + str(i) + '.jpg','wb')
fp.write(pic.content)
fp.close()
i += 1
效果同上
換個(gè)網(wǎng)站試試
#!/usr/bin/python
#-*- coding:utf-8 -*-
import re
import requests
import urllib2
#讀取源代碼文件
url = 'http://www.moko.cc/channels/post/23/1.html'
response = urllib2.urlopen(url)
htmls = response.read()
#匹配圖片網(wǎng)址
pic_url = re.findall('img src2="(.*?)" alt=',htmls,re.S)
i = 0
for each in pic_url:
print 'now downloading : ' + each
pic = requests.get(each)
fp = open('picmoko\\' + str(i) + '.jpg','wb')
fp.write(pic.content)
fp.close()
i += 1
把第一頁的48張封面圖給爬下來了~
實(shí)戰(zhàn)——處理文本
準(zhǔn)備下個(gè)電視劇,用mac的瀏覽器打開了一個(gè)頁面
然而mac沒有裝迅雷鸠儿,都是用虛擬機(jī)里面的破解版迅雷下載的屹蚊。
這個(gè)時(shí)候直接點(diǎn)頁面就尷尬了,無法調(diào)用迅雷
右鍵看了看进每,頁面源代碼
發(fā)現(xiàn)ed2k鏈接都在里面汹粤,一看就可以用正則表達(dá)式處理一下,然后復(fù)制到虛擬機(jī)的迅雷里面創(chuàng)建任務(wù)
# -*- coding:utf-8 -*-
#test111.py
__author__ = 'jerry'
import re
f = open('test111.txt','r')
html = f.read()
f.close()
web = re.findall('href="(.*?)">',html,re.S)
i=0
while i<13:
print web[i]
i+=1
復(fù)制到虛擬機(jī)的迅雷里面創(chuàng)建任務(wù)