【问题标题】:Regex returning nothing in Python正则表达式在 Python 中不返回任何内容
【发布时间】:2012-06-12 01:34:05
【问题描述】:

我是第一次使用 Python,我使用 Mechanize 搜索网站以及 BeautifulSoup 来选择特定的 div,现在我正在尝试使用正则表达式获取特定的句子。这是汤对象的内容;

    <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>

(感谢您在格式化方面的帮助)

我获取文本“Hull, Christopher Merritt”的正则表达式是;

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

它总是返回空,我不知道为什么,有人有什么想法吗?

感谢您的回答,我已将其更改为;

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

现在效果很好。

【问题讨论】:

标签: python beautifulsoup mechanize


【解决方案1】:

? 是正则表达式中的一个魔术标记,意思是前一个原子的零个或一个。由于您需要文字问号符号,因此需要对其进行转义。

【讨论】:

  • 啊,我不知道。谢谢,我是正则表达式的新手,我什至没有想到类似的东西。
【解决方案2】:

你应该在你的正则表达式中转义?

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

【讨论】:

  • 两个答案都很好,但他先回复了,抱歉。虽然感谢您的格式化帮助。
  • @user1094705 是的,我正在编辑您的帖子,而其他人正在回答您的问题。
猜你喜欢
  • 2019-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多