V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
python30
V2EX  ›  Python

用 Python 语法操作,如何用一个正则把里面面各类图片替换成,这种统一的格式?

  •  
  •   python30 · 2020-03-08 00:03:44 +08:00 · 2845 次点击
    这是一个创建于 1516 天前的主题,其中的信息可能已经有所发展或是发生改变。
    用 python 语法操作:


    如果一段文本里可能含以下图片地址与文字等组合
    <img src="http-url" alt="妹子" />
    <img src="http-url" alt="妹子" style="height:auto !important;" />
    <img src="http-url" alt="妹子" width="100%" />

    http-url 为图片地址,
    如何用一个正则把上面各类图片替换成:

    <mip-img src="http-url" alt="妹子" ></mip-img>

    这种统一的形式。

    不知道有没有一个正则可以把全部图片地址,都统一替换成:

    <mip-img src="http-url" alt="妹子" ></mip-img>

    这种格式。

    单一的好写。是全部的总是不成功。

    哪位朋友指导一下,谢谢。
    12 条回复    2020-03-11 11:00:24 +08:00
    zhzy
        1
    zhzy  
       2020-03-08 00:20:27 +08:00 via iPhone
    好奇“单一的好写但是全部的不成功”是什么情况😳
    python30
        2
    python30  
    OP
       2020-03-08 00:24:22 +08:00
    @zhzy
    只把
    <img src="http-url" alt="妹子" />
    这一个替换可以成功
    要是<img 里面还很多其它参数之类的就不成功了
    zhzy
        3
    zhzy  
       2020-03-08 00:31:52 +08:00 via iPhone
    @python30 加个 .*? 不就可以了么 关键词贪婪匹配
    PTLin
        4
    PTLin  
       2020-03-08 00:37:10 +08:00
    def foo(s):
    template='<mip-img src="{0[0]}" alt="{0[1]}" ></mip-img>'
    m=re.match(r'.*src=\"(.*?)\"\s*alt=\"(.*?)\".*',s)
    if m is not None:
    return template.format(m.groups())
    ysc3839
        5
    ysc3839  
       2020-03-08 06:26:13 +08:00 via Android
    如果没有特殊限制,建议解析 html 再处理。
    ClericPy
        6
    ClericPy  
       2020-03-08 11:47:40 +08:00
    python30
        7
    python30  
    OP
       2020-03-08 12:08:25 +08:00
    @PTLin
    @ClericPy
    谢谢。我测试一下。
    iRocW
        8
    iRocW  
       2020-03-09 17:03:09 +08:00
    只要提取 src alt 重写 不就好了?
    l4ever
        9
    l4ever  
       2020-03-10 20:57:45 +08:00
    ```
    from bs4 import BeautifulSoup

    if __name__ == "__main__":
    html = """
    <img src="http-url" alt="妹子" />
    <img src="http-url" alt="妹子" style="height:auto !important;" />
    <img src="http-url" alt="妹子" width="100%" />
    """

    soup = BeautifulSoup(html, 'html.parser')
    img = soup.find_all('img')
    for i in img:
    src = i.attrs['src']
    alt = i.attrs['alt']
    print(f'<mip-img src="{src}" alt="{alt}" ></mip-img>')

    ```
    l4ever
        10
    l4ever  
       2020-03-10 20:58:08 +08:00
    为什么回复不支持 markdown?
    l4ever
        11
    l4ever  
       2020-03-10 20:59:21 +08:00
    python30
        12
    python30  
    OP
       2020-03-11 11:00:24 +08:00
    @l4ever 谢谢。已经解决了。根据 @ClericPy 这朋友写的
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1548 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 16:26 · PVG 00:26 · LAX 09:26 · JFK 12:26
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.