Python利用正則表達式匹配標題

正則表達式基礎應用

html文本

<html>
    <body>
        <h1>新溪大橋早高峯報道:堵成一窩蜂</h1>
        <h5>是否讓白沙大橋幫助每小時前進300米的新溪大橋分流呢</h5>
        <div>
            <div class="publish">
                <p>發佈者:<span class="publisher">今日新聞</span>|發佈時間:<span class="pubTime">2020-1-29</span></p>
            </div>
            <div class="content">
                <p>新溪大橋於 2018 年 6 月正式啓用通車……</p>
                <p>……</p>
                <p>……</p>
                <p>……</p>
                <p>記者:王大力、陳小七(實習)</p>
            </div>
        </div>
    </body>
</html>

Pyhton代碼匹配標題

import re

f = open("1-6.txt", "r", encoding="UTF-8")
html = f.read()
f.close()

title = re.findall("h1>(.*?)<",html)
print(title)

結果

運行結果

注意事項

編碼方式,咱們經常使用的gbk,utf8。html

相關文章
相關標籤/搜索