V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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 · Mar 8, 2020 · 3428 views
    This topic created in 2241 days ago, the information mentioned may be changed or developed.
    用 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 replies    2020-03-11 11:00:24 +08:00
    zhzy
        1
    zhzy  
       Mar 8, 2020 via iPhone
    好奇“单一的好写但是全部的不成功”是什么情况😳
    python30
        2
    python30  
    OP
       Mar 8, 2020
    @zhzy
    只把
    <img src="http-url" alt="妹子" />
    这一个替换可以成功
    要是<img 里面还很多其它参数之类的就不成功了
    zhzy
        3
    zhzy  
       Mar 8, 2020 via iPhone
    @python30 加个 .*? 不就可以了么 关键词贪婪匹配
    PTLin
        4
    PTLin  
       Mar 8, 2020
    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  
       Mar 8, 2020 via Android
    如果没有特殊限制,建议解析 html 再处理。
    ClericPy
        6
    ClericPy  
       Mar 8, 2020
    python30
        7
    python30  
    OP
       Mar 8, 2020
    @PTLin
    @ClericPy
    谢谢。我测试一下。
    iRocW
        8
    iRocW  
       Mar 9, 2020
    只要提取 src alt 重写 不就好了?
    luckyc
        9
    luckyc  
       Mar 10, 2020
    ```
    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>')

    ```
    luckyc
        10
    luckyc  
       Mar 10, 2020
    为什么回复不支持 markdown?
    luckyc
        11
    luckyc  
       Mar 10, 2020
    python30
        12
    python30  
    OP
       Mar 11, 2020
    @l4ever 谢谢。已经解决了。根据 @ClericPy 这朋友写的
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2891 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 44ms · UTC 15:31 · PVG 23:31 · LAX 08:31 · JFK 11:31
    ♥ Do have faith in what you're doing.