標籤:不同 split() app title exp 遍曆 等等 col lan
regular expresion由一系列特定字元及其組合成的字串,用來對目標字串進行過濾操作。。
re相關知識點
pythonRegex庫為re,用import re匯入,在然後用re.compile(pattern,flag)將Regex字串編譯成Regex對象。在利用re提供的內建函數對字串進行匹配,搜尋,替換,切分和分組等操作。
flag常用的取值:
re.I 忽略大小寫,re.X 忽略空格
import redef check(string): p=re.compile("^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$",re.I) if p.match(string): print("%s符合規則"%string) else: print("%s不符合規則"%string)st1=‘[email protected]‘st2=‘[email protected]‘check(st1)check(st2)
[email protected].com符合規則[email protected].com符合規則
re.match()從起始位置匹配
re.search()搜尋整個字串匹配,搜尋成功返回起始位置和終止位置。
re.findall()以列表形式返回全部匹配的子串
>>> print(p.match(‘dAA00‘))None>>> re.match(‘adf‘,‘sdadfg‘)>>> re.search(‘adf‘,‘sdadfgadf‘)<_sre.SRE_Match object; span=(2, 5), match=‘adf‘>>>> re.findall(‘adf‘,‘sdadfgadf‘)[‘adf‘, ‘adf‘]
切分
在實際應用中,不同資料來源用不同的分隔字元,可能是空格,定位字元號,逗號等等。 利用Regex和split()函數,可以方便的分開。
re.split(pattern,string[,maxsplit])
.分隔開
>>> st=‘https:\\www.baidu.com‘>>> lt=re.split(‘\.‘,st)>>> lt[‘https:\\www‘, ‘baidu‘, ‘com‘]
逗號和空格分隔。
>>> st=‘df lx 23,77‘>>> li=re.split(‘[\s\,]‘,st)>>> li[‘df‘, ‘lx‘, ‘23‘, ‘77‘]
替換,利用re庫中sub()和subn()函數,可以將Regex所匹配的內容換成指定的字串。
sub()返回的是替換後的字串
subn()是以元群組類型還回新字串和替換的次數。
關鍵字和諧,re寫的還是有點問題
下載簡書交友專題的妹子圖片.
我已經Regex,匹配了10篇文章,但是有些沒有圖片,有些
圖片標籤匹配不對,有時間在修改了. 準備遍曆整過專題,下載所有圖片,嘻嘻,還要判斷性別,找出老鄉.
import urllib.requestimport urllib.parseimport reimport osdef get_road(url0): req=urllib.request.Request(url0) req.add_header(‘User-Agent‘, ‘Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 ‘ ‘(KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36‘) response=urllib.request.urlopen(req) html=response.read().decode("utf-8") pattern=re.compile(r‘<a class="title" target="_blank" href="(.*?)"‘) result=re.findall(pattern,html) return resultdef get_jiaoyou_url(result,s0): s=s0 return geturl(result,s)def gethtml(ur): url=ur req=urllib.request.Request(url) req.add_header(‘User-Agent‘, ‘Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 ‘ ‘(KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36‘) response=urllib.request.urlopen(req) html=response.read().decode("utf-8") return htmldef getpath(html): #reg=r‘.*?\.png‘ reg=r‘<img data-original-src="(.*?\.png)"‘ imgre=re.compile(reg) urls=imgre.findall(html) return urlsdef geturl(url,s): urls=[s+str(i) for i in url] for i in range(len(urls)): print(urls[i]) print("url_length=",len(urls)) return urlsdef download(urls): x=10 print("length=",len(urls)) for url in urls: filename=‘/home/dflx/下載/jiaoyou_photo/‘+str(x)+‘.png‘ urllib.request.urlretrieve(url,filename) x+=1 print(x)def download_all(urls): print(len(urls)) print(‘---------------‘) index=0 while index<len(urls): print(urls[index]) #download(urls[index]) index+=1 print("********")def main(): url0="https://www.jianshu.com/c/bd38bd199ec6" #ur=‘https://www.jianshu.com/p/407dac18983c‘ ur=‘https://www.jianshu.com/p/189d1b8101e6‘ html=gethtml(ur) path=getpath(html) urls=geturl(path,‘https:‘) download(urls) """ result=get_road(url0) allurls=get_jiaoyou_url(result,‘https://www.jianshu.com‘) download_all(allurls) """
下載的圖片叉車圖片
pythonRegex,以及應用[下載圖片]