2014-10-11 64 views
-2
wiki_page = urlopen('http://en.wikipedia.org/wiki/superman') 
html_code = wiki_page.read() 
headline = findall ('<h1.*><span.*>(.+)</span></h1>', html_code) 

所以我想要一個解釋,爲什麼這個例子的代碼吐出一句話「超人」,而不是從頭到尾一切從的findAll查詢開始:REG-EX解釋

爲例如,這是找到所有的代碼,下方和它自動輸出其結果:

<h1 id="firstHeading" class="firstHeading" lang="en"> 
    <span dir="auto"> 
     Superman 
    </span> 
</h1> 
+0

編輯我的@ msrd0是incorrect.There不應該是空格或換行字符,按您的正則表達式 – vks 2014-10-11 11:19:55

回答

1

由於re.findall函數試圖第一吐出捕獲組內的字符。如果沒有捕捉組存在,那麼只有它吐出匹配的字符串。

從正則表達式中移除捕獲組以顯示匹配的字符串。

<h1.*><span.*>.+?</span></h1>