我正在嘗試創建一個測試來驗證鏈接是否在網頁上呈現。這個正則表達式有什麼問題?
我不明白我在做什麼錯了這一說法測試:
self.assertRegexpMatches(response.content, r'<a href="https://stackoverflow.com/questions/?sort=elite" class="on" title="Staff Selected Questions">elite</a>')
我知道的標記在頁面上,因爲我是從response.content
我試圖複製它在Python外殼中使用正則表達式:
In [27]: links = """<div class="tabsA"><a href="https://stackoverflow.com/questions/?sort=active" title="Most recently updated questions">active</a><a href="https://stackoverflow.com/questions/?sort=newest" title="most recently asked questions">newest</a><a href="https://stackoverflow.com/questions/?sort=hottest" title="most active questions in the last 24 hours">hottest</a><a href="https://stackoverflow.com/questions/?sort=mostvoted" title="most voted questions">most voted</a><a href="https://stackoverflow.com/questions/?sort=elite" class="on" title="Staff Selected Questions">elite</a></div>"""
In [28]: re.search(r'<a href="https://stackoverflow.com/questions/?sort=elite" class="on" title="Staff Selected Questions">elite</a>', links)
由於某種原因,它不工作。
如何創建正則表達式使其起作用?
你必須逃避問號。所以它的re.search(r'somestuff \?somemorestuff') – PeterT 2011-06-03 20:23:58
舌頭在臉頰響應:它是什麼錯是你試圖使用正則表達式來解析標記。 ;-) – 2011-06-03 20:24:18