首先要連接自己的數據庫
import pymysql import requests #需要導入模塊 db = pymysql.connect('localhost', 'root', '*********', 'mysql')#第三個是數據庫密碼,第四個是數據庫名稱 print("數據庫連接成功!") print("---------------------------------------------------") r = requests.get("https://python123.io/ws/demo.html")#獲取網頁源代碼 print(r.text)
幾個基本操作
r = requests.get("https://python123.io/ws/demo.html")#獲取網頁源代碼 print(r)#輸出該網頁請求是否成功,成功輸出<Response [200]> print(r.text)# 以文本形式輸出網頁源代碼(格式和網頁源代碼一樣) print(r.content)#以二進制形式輸出源代碼(沒有換行和空格) print(r.encoding)# 輸出網頁編碼方式 print(r.apparent_encoding)#和r.encoding功能相同,但更為精准 print(r.status_code)# 打印狀態碼, HTTP請求的返回狀態,200表示連接成功,404表示失敗 print(r.raise_for_status())# 若正常捕獲網頁內容,輸出 None表示無異常
import re庫
一、re.search(匹配規則,要匹配的字符串名稱)
功能:掃描整個字符串返回第一個成功匹配的結果
result.group()獲取匹配的結果
result.span()獲去匹配字符串的長度范圍
re.group(1)獲取第一個括號中匹配的結果
import pymysql import requests #需要導入模塊 db = pymysql.connect('localhost', 'root', '********', 'mysql')#第三個是數據庫密碼,第四個是數據庫名稱 print("數據庫連接成功!") print("---------------------------------------------------") r = requests.get("https://python123.io/ws/demo.html")#獲取網頁源代碼 import re def get_text(url):#函數 r = requests.get(url) r.raise_for_status() r.encoding = r.apparent_encoding return r.text print("-------------1-------------") print(get_text('https://python123.io/ws/demo.html'))#輸出網頁源代碼 demo = get_text('https://python123.io/ws/demo.html')#類似於數組賦值 #demo類似於一個數組名字 result = re.search('Th.*?ge', demo)#賦值 print("-------------2-------------") print(result)#輸出匹配字符串的長度范圍和匹配的結果 print("-------------3-------------") print(result.group())#只輸出獲取匹配的結果 print("-------------4-------------") print(result.span())#輸出獲取匹配字符串的長度范圍
輸出
-------------1------------- <html><head><title>This is a python demo page</title></head> <body> <p class="title"><b>The demo python introduces several python courses.</b></p> <p class="course">Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses: <a href="http://www.icourse163.org/course/BIT-268001" class="py1" id="link1">Basic Python</a> and <a href="http://www.icourse163.org/course/BIT-1001870001" class="py2" id="link2">Advanced Python</a>.</p> </body></html> -------------2------------- <re.Match object; span=(19, 45), match='This is a python demo page'> -------------3------------- This is a python demo page -------------4------------- (19, 45) -------------5------------- ['<p class="title"><b>The demo python introduces several python courses.</b></p>', '<p class="course">Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:\r\n<a href="http://www.icourse163.org/course/BIT-268001" class="py1" id="link1">Basic Python</a> and <a href="http://www.icourse163.org/course/BIT-1001870001" class="py2" id="link2">Advanced Python</a>.</p>'] Process finished with exit code 0
二、re.match(匹配規則,要匹配的字符串名稱,匹配成功返回值)
功能:re.match()功能和re.search()一樣,但是強調從字符串的起始位置匹配一個模式,如果不是起始位置匹配的話,match()就會返回None
一般使用re.search(),不用re.match()
語法格式:
re.match(pattern,string,flags=0)
三、re.findall(匹配規則,要匹配的字符串名稱,re.s)---------re.s是輸出回車換行,匹配到一個結果,輸出一個換行
功能:搜索字符串,以列表(list)的形式返回全部能匹配的子串-------->print(result)
import re html = '''<div id="songs-list"> <h2 class="title">經典老歌</h2> <p class="introduction"> 經典老歌列表 </p> <ul id="list" class="list-group"> <li data-view="2">一路上有你</li> <li data-view="7"> <a href="/2.mp3" singer="任賢齊">滄海一聲笑</a> </li> <li data-view="4" class="active"> <a href="/3.mp3" singer="齊秦">往事隨風</a> </li> <li data-view="6"><a href="/4.mp3" singer="beyond">光輝歲月</a></li> <li data-view="5"><a href="/5.mp3" singer="陳慧琳">記事本</a></li> <li data-view="5"> <a href="/6.mp3" singer="鄧麗君">但願人長久</a> </li> </ul> </div>''' results = re.findall('<li.*?href="(.*?)".*?singer="(.*?)">(.*?)</a>', html, re.S) print(results)#不換行輸出所有匹配的內容 print(type(results))#type(results)返回results的數據類型(列表list) for result in results: print(result)#以列表形式輸出所有匹配內容,包括括號 print(result[0], result[1], result[2])#以列表形式依次返回 括號內 匹配的內容,不包括括號
輸出結果
[('/2.mp3', '任賢齊', '滄海一聲笑'), ('/3.mp3', '齊秦', '往事隨風'), ('/4.mp3', 'beyond', '光輝歲月'), ('/5.mp3', '陳慧琳', '記事本'), ('/6.mp3', '鄧麗君', '但願人長久')] <class 'list'> ('/2.mp3', '任賢齊', '滄海一聲笑') /2.mp3 任賢齊 滄海一聲笑 ('/3.mp3', '齊秦', '往事隨風') /3.mp3 齊秦 往事隨風 ('/4.mp3', 'beyond', '光輝歲月') /4.mp3 beyond 光輝歲月 ('/5.mp3', '陳慧琳', '記事本') /5.mp3 陳慧琳 記事本 ('/6.mp3', '鄧麗君', '但願人長久') /6.mp3 鄧麗君 但願人長久 [Finished in 0.1s]
幾種匹配規則:
1、泛匹配
^:開始匹配標志
$:匹配結束標志

import re content= "hello 123 4567 World_This is a regex Demo" result = re.match("^hello.*Demo$",content) print(result) print(result.group()) print(result.span())
輸出

<re.Match object; span=(0, 41), match='hello 123 4567 World_This is a regex Demo'> hello 123 4567 World_This is a regex Demo (0, 41) [Finished in 0.1s]
2、目標匹配
如果為了匹配字符串中具體的目標,則需要通過()括起來,()內就是要匹配輸出的內容:

import re content= "hello 1234567 World_This is a regex Demo" result = re.match('^hello\s(\d+)\sWorld.*Demo$',content) print(result) print(result.group()) print(result.group(1)) print(result.span())
輸出

<re.Match object; span=(0, 40), match='hello 1234567 World_This is a regex Demo'> hello 1234567 World_This is a regex Demo 1234567 (0, 40) [Finished in 0.1s]
3、貪婪匹配
.* :盡可能多的匹配非目標字符,將輸出目標字符長度匹配到最小
.*? :盡可能少的匹配非目標字符,將輸出目標字符長度匹配到最大
注:按目標類型分界
.* :

import re content= "hello 1234567 World_This is a regex Demo" result= re.match('^hello.*(\d+).*Demo',content) print(result) print(result.group(1))
輸出

<re.Match object; span=(0, 40), match='hello 1234567 World_This is a regex Demo'> 7 [Finished in 0.1s]
.*? :

import re content= "hello 1234567 World_This is a regex Demo" result= re.match('^hello.*?(\d+).*Demo',content) print(result) print(result.group(1))
輸出

<re.Match object; span=(0, 40), match='hello 1234567 World_This is a regex Demo'> 1234567] [Finished in 0.1s
4、常規匹配(比較繁瑣,不常用)

import re content= "hello 123 4567 World_This is a regex Demo" result = re.match('^hello\s\d\d\d\s\d{4}\s\w{10}.*Demo$',content) print(result)#輸出源代碼 print(result.group())#輸出匹配內容 print(result.span())#輸出匹配代碼的長度
輸出

<re.Match object; span=(0, 41), match='hello 123 4567 World_This is a regex Demo'> hello 123 4567 World_This is a regex Demo (0, 41) [Finished in 0.1s]
正則表達式
常用的匹配模式: \w 匹配字母數字及下划線 \W 匹配f非字母數字下划線 \s 匹配任意空白字符,等價於[\t\n\r\f] \S 匹配任意非空字符 \d 匹配任意數字 \D 匹配任意非數字 \A 匹配字符串開始 \Z 匹配字符串結束,如果存在換行,只匹配換行前的結束字符串 \z 匹配字符串結束 \G 匹配最后匹配完成的位置 \n 匹配一個換行符 \t 匹配一個制表符 ^ 匹配字符串的開頭 $ 匹配字符串的末尾 . 匹配任意字符,除了換行符,re.DOTALL標記被指定時,則可以匹配包括換行符的任意字符 [....] 用來表示一組字符,單獨列出:[amk]匹配a,m或k [^...] 不在[]中的字符:[^abc]匹配除了a,b,c之外的字符 * 匹配0個或多個的表達式 + 匹配1個或者多個的表達式 ? 匹配0個或1個由前面的正則表達式定義的片段,非貪婪方式 {n} 精確匹配n前面的表示 {m,m} 匹配n到m次由前面的正則表達式定義片段,貪婪模式 a|b 匹配a或者b () 匹配括號內的表達式,也表示一個組 [\u4e00-\u9fa5] :匹配中文 (\d{4}-\d{2}-\d{2}) : 匹配日期 .*? :匹配任意字符串 \[(\d{4}-\d{2}-\d{2})\] 匹配時間 eg:[2019-03-12] for content in contents: try: # 替換文本 s = str(content).replace('y,','')#.replace('<', '-5') s = s.replace('年', '-').replace('月', '-').replace('日', '') s = s.replace('(', '').replace(')', '').replace('\'', '') content = s.split(',') # s = str(content).replace('/','-') # # s = re.sub('(\d{4}-\d{2})',r'\1-',s) # s = s.replace('(', '').replace(')', '').replace('\'', '') # content = s.split(',') list.append(content) except EOFError as e: print(e) continue return list
正則表達式匹配練習
1、匹配貓眼電影top100的電影名、主演、上映日期
對應正則表達式:'class="name".*?title="(.*?)".*?:(.*?)\s*?</p>.*?:(\d{4}-\d{2}-\d{2})'
2、匹配貓眼電影top100的海報圖片
對應的正則表達式:'img\sdata-src="(.*?)"\salt'
3、匹配西南大學計算機學院講座信息
'<li><span\sclass="fr">\[(\d{4}-\d{2}-\d{2})\].*? (.*?)</a></li>',