我正在尝试使用 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" />...
我试图抓住两者之间的价值观<title>
and </title>
标签。它应该相当简单,但它不起作用。这是我的 python 代码。
result = re.search('\<title\>(.+?)\</title\>', html)
if result is not None:
title = result.group(0)
无论出于何种原因,这都不适用于本文。它将 result.group() 返回为 None,否则我会收到 AttributeError。 AttributeError:“NoneType”对象没有属性“groups”
我已将此文本 C&P 放入在线 python 正则表达式开发人员中,并尝试了所有选项(re.match、re.findall、re.search),它们在那里工作,但无论出于何种原因,在我的脚本中它无法找到之间的任何内容这些标签。甚至尝试其他正则表达式,例如
<title>(.*?)</title>
etc
您应该使用多塔尔旗使.
也匹配换行符。
result = re.search('\<title\>(.+?)\</title\>', html, re.DOTALL)
正如文档所说:
...没有这个标志,'.'
会匹配任何东西except换行符
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)