漂亮的汤 python 3.7 DeprecationWarning

2024-03-28 17:57:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我有python3.7,我使用beautifulsoup4(4.8.1)从HTML中删除样式。你知道吗

但我得到一个警告:

../../../../.local/share/virtualenvs/backend-HTC-FyOn/lib/python3.7/site-packages/bs4/element.py:167: in formatter_for_name if isinstance(formatter, collections.Callable):

E DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated since Python 3.3,and in 3.9 it will stop working

../../../../.local/share/virtualenvs/backend-HTC-FyOn/lib/python3.7/collections/init.py:52: DeprecationWarning

from bs4 import BeautifulSoup


def clean_html(html):
    string = """
    <p>
    <style type=""text/css"">
    P { margin-bottom: 0.08in; direction: ltr; widows: 2; orphans: 2; }A:link { color: rgb(0, 0, 255); }    </style>
    </p>
    <p style=""font-variant: normal; font-style: normal; font-weight: normal"">
        <font face=""Trebuchet MS, Arial, Verdana, sans-serif""><span   style=""font-size: 12px; background-color: rgb(238, 238, 238);"">blablabla. </span></font></p>
    <p style=""font-variant: normal; font-style: normal; font-weight: normal"">
    <font face=""Trebuchet MS, Arial, Verdana, sans-serif""><span style=""font-size: 12px; background-color: rgb(238, 238, 238);"">tjatjatja</span></font><span style=""font-family: 'Trebuchet MS', Arial, Verdana, sans-serif; font-size: 12px; background-color: rgb(238, 238, 238);"">tjetjetje</span><span style=""font-size: 12px; font-family: 'Trebuchet MS', Arial, Verdana, sans-serif; background-color: rgb(238, 238, 238);"">.</span></p>
    <p style=""font-variant: normal; font-style: normal; font-weight: normal"">
    <span style=""font-family: 'Trebuchet MS', Arial, Verdana, sans-serif; font-size: 12px; background-color: rgb(238, 238, 238);"">huehuehue</span></p>
    """
    soup = BeautifulSoup(string)
    [s.extract() for s in soup('style')]

    return str(soup)

我使用的是最新版本:

 "beautifulsoup4": {
            "hashes": [
                "sha256:5279c36b4b2ec2cb4298d723791467e3000e5384a43ea0cdf5d45207c7e97169",
                "sha256:6135db2ba678168c07950f9a16c4031822c6f4aec75a65e0a97bc5ca09789931",
                "sha256:dcdef580e18a76d54002088602eba453eec38ebbcafafeaabd8cab12b6155d57"
            ],
            "index": "pypi",
            "version": "==4.8.1"
        },

在执行str(soup)时出错


Tags: insizestylergbmscolorbackgroundspan