python正則表達式,以及應用[下載圖片]


regular expresion由一系列特定字符及其組合成的字符串,用來對目標字符串進行過濾操作。。

re相關知識點

python正則表達式庫為re,用import re導入,在然后用re.compile(pattern,flag)將正則表達式字符串編譯成正則表達式對象。在利用re提供的內置函數對字符串進行匹配,搜索,替換,切分和分組等操作。

flag常用的取值:
re.I 忽略大小寫,re.X 忽略空格

import re def check(string): p=re.compile("^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$",re.I) if p.match(string): print("%s符合規則"%string) else: print("%s不符合規則"%string) st1='dflx@163.com' st2='123456@qq.com' check(st1) check(st2)
dflx@163.com符合規則 123456@qq.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']

切分
在實際應用中,不同數據源用不同的分隔符,可能是空格,制表符號,逗號等等。 利用正則表達式和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()函數,可以將正則表達式所匹配的內容換成指定的字符串。
sub()返回的是替換后的字符串
subn()是以元組類型還回新字符串和替換的次數。

關鍵字和諧,re寫的還是有點問題

下載簡書交友專題的妹子圖片.

我已經正則表達式,匹配了10篇文章,但是有些沒有圖片,有些
圖片標簽匹配不對,有時間在修改了. 准備遍歷整過專題,下載所有圖片,嘻嘻,還要判斷性別,找出老鄉.

import urllib.request import urllib.parse import re import os def 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 result def 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 html def getpath(html): #reg=r'.*?\.png' reg=r'<img data-original-src="(.*?\.png)"' imgre=re.compile(reg) urls=imgre.findall(html) return urls def 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 urls def 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) """

下載的圖片叉車圖片
圖片描述


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM