2012-06-12 81 views
0

我正在Python中第一次工作,我已經使用Mechanize搜索一個網站以及BeautifulSoup來選擇特定的div,現在我試圖抓住一個特定的句子,表達。這是湯物的內容;正則表達式在Python中沒有返回任何東西

<div id="results"> 
    <table cellspacing="0" width="100%"> 
    <tr> 
     <th align="left" valign="middle" width="32%">Physician Name, (CPSO#)</th> 
     <th align="left" valign="middle" width="36%">Primary Practice Location</th> 
     <!-- <th width="16%" align="center" valign="middle">Accepting New Patients?</th> --> 
     <th align="center" valign="middle" width="32%">Disciplinary Info &amp; Restrictions</th> 
    </tr> 

    <tr> 
     <td> 
      <a class="doctor" href="details.aspx?view=1&amp;id= 85956">Hull, Christopher Merritt </a> (#85956) 
     </td> 
     <td>Four Counties Medical Clinic<br/>1824 Concessions Dr<br/>Newbury ON N0L 1Z0<br/>Phone: (519) 693-0350<br/>Fax: (519) 693-0083</td> 
     <!-- <td></td> --> 
     <td align="center"></td> 
    </tr> 
    </table> 
</div> 

(感謝您與格式幫助)

我的正則表達式來獲取文本「赫爾,克里斯托弗·梅里特」的;

patFinderName = re.compile('<a class="doctor" href="details.aspx?view=1&amp;id= 85956">(.*) </a>') 

它一直返回空,我不明白爲什麼,有人有任何想法?

謝謝你的答案,我已經改變了;

patFinderName = re.compile('<a class="doctor" href=".*">(.*) </a>') 

現在它工作得很好。

+0

你需要躲避'''後aspx'? –

+0

[用於HTML解析的Python正則表達式(BeautifulSoup)](http://stackoverflow.com/q/55391/),[Python Reg Ex。問題](http://stackoverflow.com/q/90052/),並可能[其他](http://stackoverflow.com/search?q=%2Bbeautifulsoup+%2Bfind+%2Belement&submit=search) – outis

回答

3

?是正則表達式中的魔法令牌,意味着零或前一個原子之一。當你想要一個文字問號符號,你需要逃避它。

+0

啊,我沒有理念。謝謝你,我是新來的正規表達式,而那些東西甚至還沒有跨過我的腦海。 – user1094705

0

你應該逃避你的正則表達式的?

In [8]: re.findall('<a class="doctor" href="details.aspx\?view=1&amp;id= 85956">(.*)</a>', text) 
Out[8]: ['Hull, Christopher Merritt '] 
+0

這兩個答案都很棒,但他首先回應,對不起。雖然感謝您的格式幫助。 – user1094705

+0

@ user1094705是的,我在編輯你的文章,而其他人回答你的問題。 – satoru

相關問題