首页 > 解决方案 > Python - 将文本链接转换为 html

问题描述

def linkify(msg):
    for m in msg.split(" "):
        m = m.lower()
        if m.startswith("https:") or m.startswith("http:"):
            msg = re.sub(m, f"<a href='{m}' target='_blank'>{m}</a>", msg, re.I)
    return msg

我想要做的是将普通链接制作成 html 链接。虽然忽略了用户可能会或可能不会发送的 html 链接。我正在使用这个测试字符串:hi how are you https://google.com/img https://google.com/img/l <a href='https://google.com/img' target='_blank'>https://google.com/img</a> hi <a href='http://test.com/img' target='_blank'>http://test.com/img</a> <a href='https://test.com/hi/lol' target='_blank'>https://test.com/hi/lol</a>

输出

hi how are you <a href='https://google.com/img' target='_blank'>https://google.com/img</a> <a href='https://google.com/img' target='_blank'>https://google.com/img</a>/l <a href='https://google.com/img' target='_blank'>https://google.com/img</a> hi <a href='http://test.com/img' target='_blank'>http://test.com/img</a> <a href='https://test.com/hi/lol' target='_blank'>https://test.com/hi/lol</a>

注意链接:<a href='https://google.com/img' target='_blank'>https://google.com/img</a>/l/1完全忽略,我不知道为什么

期望的输出

 <a href='https://google.com/img' target='_blank'>https://google.com/img</a> <a href='https://google.com/img/1' target='_blank'>https://google.com/img/1</a> <a href='https://google.com/img' target='_blank'>https://google.com/img</a> hi <a href='http://test.com/img' target='_blank'>http://test.com/img</a> <a href='https://test.com/hi/lol' target='_blank'>https://test.com/hi/lol</a>

标签: pythonpython-3.xregex

解决方案


您可以使用

import regex as re

text = """hi how are you https://google.com/img https://google.com/img/l <a href='https://google.com/img' target='_blank'>https://google.com/img</a> hi <a href='http://test.com/img' target='_blank'>http://test.com/img</a> <a href='https://test.com/hi/lol' target='_blank'>https://test.com/hi/lol</a>"""

rx = re.compile(r'<a.*?</a>(*SKIP)(*FAIL)|\bhttps?://\S+')
text = rx.sub(lambda m: f"<a href='{m.group(0)}' target='_blank'>{m.group(0)}</a>", text)

print(text)

哪个产量

hi how are you <a href='https://google.com/img' target='_blank'>https://google.com/img</a> <a href='https://google.com/img/l' target='_blank'>https://google.com/img/l</a> <a href='https://google.com/img' target='_blank'>https://google.com/img</a> hi <a href='http://test.com/img' target='_blank'>http://test.com/img</a> <a href='https://test.com/hi/lol' target='_blank'>https://test.com/hi/lol</a>

请参阅regex101.com 上的表达式演示。


推荐阅读