You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python的re.findall()提取含target参数的完整<a>标签?

提取包含target参数的完整标签解决方案

直接用Python的re.findall()结合正则表达式就能解决,代码示例如下:

import re

text = '&lt;a href=&quot;https://url1.com/q234243/&quot; target=&quot;_blank&quot;&gt;la orden  texto, texto ,texto jurídicas del bla bla bla que obliga a la corporación que rige al  tenis bla bla bla, el patrocinador de sus principales asuntos y&lt;a href=&quot;https://url2.com/124345w23/&quot; target=&quot;_blank&quot;&gt; el dictamen de texto random y mas texto random apu, que se han convertido en uno de los rammstein es la ostia económicos de la actividad, al punto de'

# 匹配包含target="_blank"的完整<a>标签的正则
pattern = r'&lt;a[^>]*?target=&quot;_blank&quot;[^>]*&gt;'
urls = re.findall(pattern, text)

print(urls)

运行后输出结果就是你需要的格式:

['&lt;a href=&quot;https://url1.com/q234243/&quot; target=&quot;_blank&quot;&gt;', '&lt;a href=&quot;https://url2.com/124345w23/&quot; target=&quot;_blank&quot;&gt;']

正则表达式拆解

  • &lt;a:匹配转义后的<a标签开头
  • [^>]*?:匹配任意非>的字符,*?是非贪婪模式,避免从第一个<a直接匹配到最后一个>
  • target=&quot;_blank&quot;:精准匹配转义后的target参数部分
  • [^>]*&gt;:匹配到转义后的标签结束符>为止

内容的提问来源于stack exchange,提问作者cristobal veas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 21:43:26