Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如何从这一行捕获 AttributeError 并向用户打印消息,比如说“你不能更改 class="post"”
我尝试尝试,除了 AttributeError、raise、print,但它总是什么都不返回。这就是我所做的:
re.search()None如果字符串中没有位置与模式匹配,则返回。检查None并引发异常:
re.search()
None
post_start = re.search('<div class="post">', html) if post_start is None: raise AttributeError('you can\'t change <div class="post">')