1. 程式人生 > >正則式分段提取中文,看不懂啊,先記錄下來

正則式分段提取中文,看不懂啊,先記錄下來

#-*- coding: UTF-8 -*-
import re


s = '''<html>
 <head>
 </head>
 <body>.....
 <li>......</li>
 <h2>
 我需要的內容h2
 </h2>
 <p>
 我需要的內容p
 </p>
 <h3>
 我需要的內容h3
 </h3>'''


res = r'.*?<h2>(?P<H2>.*?)</h2>.*?<p>(?P<P>.*?)</p>(?P<H3>.*?)</h3>'
target = re.compile(res, re.S | re.M)
match = target.search(s)
print(match)
if match:
    for k in match.groupdict().keys():
        print(k, ': ', match.groupdict()[k])
        print('=====================')