如何用Python的re.findall()提取含target参数的完整<a>标签?
提取包含target参数的完整标签解决方案
直接用Python的re.findall()结合正则表达式就能解决,代码示例如下:
import re text = '<a href="https://url1.com/q234243/" target="_blank">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<a href="https://url2.com/124345w23/" target="_blank"> 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'<a[^>]*?target="_blank"[^>]*>' urls = re.findall(pattern, text) print(urls)
运行后输出结果就是你需要的格式:
['<a href="https://url1.com/q234243/" target="_blank">', '<a href="https://url2.com/124345w23/" target="_blank">']
正则表达式拆解
<a:匹配转义后的<a标签开头[^>]*?:匹配任意非>的字符,*?是非贪婪模式,避免从第一个<a直接匹配到最后一个>target="_blank":精准匹配转义后的target参数部分[^>]*>:匹配到转义后的标签结束符>为止
内容的提问来源于stack exchange,提问作者cristobal veas
相关产品推荐
相关产品推荐

