我想用正则表达式从HTML页面中提取标题。目前我有这个:
title = re.search('<title>.*</title>', html, re.IGNORECASE).group()
if title:
title = title.replace('<title>', '').replace('</title>', '')
是否有正则表达式来提取<title>的内容,这样我就不必删除标签了?
我想用正则表达式从HTML页面中提取标题。目前我有这个:
title = re.search('<title>.*</title>', html, re.IGNORECASE).group()
if title:
title = title.replace('<title>', '').replace('</title>', '')
是否有正则表达式来提取<title>的内容,这样我就不必删除标签了?
当前回答
re.search('<title>(.*)</title>', s, re.IGNORECASE).group(1)
其他回答
Try:
title = re.search('<title>(.*)</title>', html, re.IGNORECASE).group(1)
我可以向您推荐美丽汤吗?Soup是一个解析所有html文档的很好的库。
soup = BeatifulSoup(html_doc)
titleName = soup.title.name
我需要一些东西来匹配package-0.0.1(名称,版本),但想拒绝一个无效的版本,如0.0.010。
参见regex101示例。
import re
RE_IDENTIFIER = re.compile(r'^([a-z]+)-((?:(?:0|[1-9](?:[0-9]+)?)\.){2}(?:0|[1-9](?:[0-9]+)?))$')
example = 'hello-0.0.1'
if match := RE_IDENTIFIER.search(example):
name, version = match.groups()
print(f'Name: {name}')
print(f'Version: {version}')
else:
raise ValueError(f'Invalid identifier {example}')
输出:
Name: hello
Version: 0.0.1
Krzysztof krasosk目前投票最多的答案是<title>a</title><title>b</title>。此外,它忽略了跨越行边界的标题标签,例如,由于行长原因。最后,它失败于<title >a</title>(这是有效的XML/HTML标记中的空白)。
因此,我提出以下改进建议:
import re
def search_title(html):
m = re.search(r"<title\s*>(.*?)</title\s*>", html, re.IGNORECASE | re.DOTALL)
return m.group(1) if m else None
测试用例:
print(search_title("<title >with spaces in tags</title >"))
print(search_title("<title\n>with newline in tags</title\n>"))
print(search_title("<title>first of two titles</title><title>second title</title>"))
print(search_title("<title>with newline\n in title</title\n>"))
输出:
with spaces in tags
with newline in tags
first of two titles
with newline
in title
最后,我和其他人一起推荐一个HTML解析器——不仅要处理HTML标记的非标准使用。
尝试使用捕获组:
title = re.search('<title>(.*)</title>', html, re.IGNORECASE).group(1)