我正在尝试创建一个测试来验证链接是否在网页上呈现。这个正则表达式有什么问题?
我不明白我在做什么错了这一说法测试:
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