gpt4 book ai didi

python - 如何从 HTML 中删除
  • 标签的内容
  • 转载 作者:行者123 更新时间:2023-11-30 23:28:56 25 4
    gpt4 key购买 nike

    我正在尝试删除 < li> 的内容标签

    我的 HTML

        <ul id="MenuGreyBar">                        
    <li style="left: 0px;">
    <a href="#" class="bgGrey">&nbsp;</a>
    </li>
    </ul>

    <ul>
    <li>
    <a href="about_us.html" class="bgLightBlue">About Us</a>
    </li>
    <li >
    <a href="Help_Support.html" class="bgMuddyGreen">Help & Support</a>
    </li>
    <li >
    <a href="Law_Info.html" class="bgGreen">Law & Information</a>
    </li>
    <!-- ... There are a few more. -->
    </ul>

    我需要删除<li>里面的所有东西标签

    我得到的代码

    最佳答案

    你的处理方式是错误的;只需搜索 li 标签并调用 .decompose()关于他们:

    soup = BeautifulSoup(input_document)
    for li in soup.find_all('li'):
    li.decompose()

    演示:

    >>> from bs4 import BeautifulSoup
    >>> input_document = '''\
    ... <ul id="MenuGreyBar">
    ... <li style="left: 0px;">
    ... <a href="#" class="bgGrey">&nbsp;</a>
    ... </li>
    ... </ul>
    ...
    ... <ul>
    ... <li>
    ... <a href="about_us.html" class="bgLightBlue">About Us</a>
    ... </li>
    ... <li >
    ... <a href="Help_Support.html" class="bgMuddyGreen">Help & Support</a>
    ... </li>
    ... <li >
    ... <a href="Law_Info.html" class="bgGreen">Law & Information</a>
    ... </li>
    ... <!-- ... There are a few more. -->
    ... </ul>
    ... '''
    >>> soup = BeautifulSoup(input_document)
    >>> for li in soup.find_all('li'):
    ... li.decompose()
    ...
    >>> print soup
    <html><head></head><body><ul id="MenuGreyBar">

    </ul>

    <ul>



    <!-- ... There are a few more. -->
    </ul>
    </body></html>

    关于python - 如何从 HTML 中删除 <li> 标签的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21405036/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com