Python 我正在尝试创建正则表达式以包含文本而不是href

Python 我正在尝试创建正则表达式以包含文本而不是href,python,html,regex,Python,Html,Regex,我正在尝试创建一个正则表达式,它将包含HTML中的文本,而不是web地址 代码如下: <h2 class="story-heading"><a href="http://web.archive.org/web/20171012234502/https://www.nytimes.com/2017/10/12/us/politics/trump-obamacare-executive-order-health-insurance.html">Trump Allows for

我正在尝试创建一个正则表达式,它将包含HTML中的文本,而不是web地址

代码如下:

<h2 class="story-heading"><a href="http://web.archive.org/web/20171012234502/https://www.nytimes.com/2017/10/12/us/politics/trump-obamacare-executive-order-health-insurance.html">Trump Allows for Cheaper, Less Regulated Health Plans</a></h2>

我尝试过使用正则表达式:
(.*)

但我不知道如何删除该行的href部分,只包含文本?所以我只需要返回“特朗普允许更便宜、监管更少的医疗计划”

您可以尝试以下方法:

import re
s = '<h2 class="story-heading"><a href="http://web.archive.org/web/20171012234502/https://www.nytimes.com/2017/10/12/us/politics/trump obamacare-executive-order-health-insurance.html">Trump Allows for Cheaper, Less Regulated Health Plans</a></h2>'
final_data = re.findall('>([a-zA-Z,\s]+)</a></h2>', s)[0]

此表达式应该满足您的要求

<h2 class="story-heading"><a.*?>(.*?)<\/a><\/h2>
直到我们找到相应链接标签和标题标签的开头

<\/a><\/h2>


注意:这里的反斜杠对于您正在使用的内容可能不是必需的,我添加它们是因为它们在我正在使用的工具中是必需的,这也可能有助于您理解我的解释是否不充分:

您最好使用一个可用的html解析器来解析html,然后遍历结果,而不是尝试手动解码。我们必须使用提供给我们的正则表达式测试仪。有没有什么方法可以纯粹作为正则表达式来实现这一点?你可以用正则表达式来近似它,但它可能不会处理所有情况。(.*)这个正则表达式对你很有帮助。请你解释一下它的各个部分,以便我可以为其他实例重用/编辑代码?
<h2 class="story-heading">
<a.*?>
(.*?)
<\/a><\/h2>