2016-12-15 68 views
-3

我有以下問題:我有一些HTML代碼,我需要獲取標籤內容。我不想使用數百萬個子字符串或類似的東西。我想使用正則表達式,但我有與過濾標籤類,ids,或沒有任何問題的問題。這裏是我的正則表達式:Python的正則表達式得到標籤內容沒有標籤名稱

match = re.search('(?<=<span(.+)?>)(.*)(?=</span>)', '<span class="red">color</span>') 

的Python會引發以下錯誤

sre_constants.error: look-behind requires fixed-width pattern 

我想從幫助從

<span class="red">color</span> 

,並獲得內容從

<span>color</span> 

謝謝大家!

+0

[RegEx match open tags not except XHTML self-contained tags](http://stackoverflow.com/questions/ 1732348/regex-match-open-tags-except-xhtml-self-contained-tags) – arco444

+1

*爲什麼*人們堅持使用正則表達式來解析HTML? –

+1

你必須使用正則表達式嗎?爲什麼不是像美麗湯 – Tobey

回答

0

簡單的回答:使用findall,跳過後視並獲取捕獲組。

<span(.+)?>(.*?)</span> 

但這失敗在很多情況下。例如。嵌套標記,包含文本的字符串</span>,依此類推...