Python Regex无法找到子字符串,但它应该

Rei*_*rne 1 python regex

我试图使用BeautifulSoup解析html尝试提取网页标题.有时这不起作用,因为网站写得不好,例如Bad End标签.如果这不起作用,我去手动正则表达式

我有文字

<html xmlns="http://www.w3.org/1999/xhtml"\n      xmlns:og="http://ogp.me/ns#"\n      xmlns:fb="https://www.facebook.com/2008/fbml">\n<head>\n    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>\n    <title>\n                    .@wolfblitzercnn prepping questions for the Cheney intvw. @CNNSitRoom today. 5p. \n            </title>\n    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />...
Run Code Online (Sandbox Code Playgroud)

我试图抓住<title></title>标签之间的值.它应该相当简单,但它不起作用.这是我的python代码.

result = re.search('\<title\>(.+?)\</title\>', html)
if result is not None:
    title = result.group(0)
Run Code Online (Sandbox Code Playgroud)

无论出于何种原因,这都不适用于本文.它返回result.group()为None或我得到一个AttributeError.AttributeError:'NoneType'对象没有属性'groups'

我已经将这个文本C&P加入到在线python正则表达式开发人员中并尝试了所有选项(re.match,re.findall,re.search)并且他们在那里工作但是无论出于何种原因我的脚本中它无法找到任何东西.这些标签.甚至尝试其他正则表达式如

<title>(.*?)</title>
Run Code Online (Sandbox Code Playgroud)

等等

Jun*_*uxx 5

您应该使用dotall标志来创建.匹配换行符.

result = re.search('\<title\>(.+?)\</title\>', html, re.DOTALL)
Run Code Online (Sandbox Code Playgroud)

正如文件所说:

...没有此标志,'.'将匹配换行符之外的任何内容