python scrape html字体标记

2024-07-08 16:04:42 发布

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

我对编程特别是python不太熟悉。我无法从html中获取字体标记文本。这是我的代码。我需要提取其中的所有文本并进行计数。因为我不知道我在运行什么程序。在

from bs4 import BeautifulSoup

html = """<P STYLE="margin-bottom: 0in">&quot;amy in marketing press one amanda in groups press two to repeat this menu press star&quot;</P>
<P STYLE="margin-bottom: 0in"><BR>
</P>
<P STYLE="margin-bottom: 0in">Labels:<FONT COLOR="#ff0000">Machine-Message,In-House-Alternative,Company-Alternative;</FONT></P>
<P STYLE="margin-bottom: 0in"><FONT COLOR="#00b050">Machine-Message,</FONT><FONT COLOR="#00b050">Greetings-Other;</FONT></P>
<P STYLE="margin-bottom: 0in"><FONT COLOR="#0070c0">Machine-Message,</FONT>
<FONT COLOR="#0070c0">Personal-Information;</FONT></P>
<P STYLE="margin-bottom: 0in"><BR>
</P>"""

soup = BeautifulSoup(html)
print(soup.find('FONT', COLOR="#ff0000"))

Tags: inmargin文本brmessagestylehtmlmachine
1条回答
网友
1楼 · 发布于 2024-07-08 16:04:42

您缺少引号“,并在中使用小写标记名汤。找或者为了得到所有发生的事情

from bs4 import BeautifulSoup

html = """<P STYLE="margin-bottom: 0in">&quot;amy in marketing press one amanda in groups press two to repeat this menu press star&quot;</P>
<P STYLE="margin-bottom: 0in"><BR>
</P>
<P STYLE="margin-bottom: 0in">Labels:<FONT COLOR="#ff0000">Machine-Message,In-House-Alternative,Company-Alternative;</FONT></P>
<P STYLE="margin-bottom: 0in"><FONT COLOR="#00b050">Machine-Message,</FONT><FONT COLOR="#00b050">Greetings-Other;</FONT></P>
<P STYLE="margin-bottom: 0in"><FONT COLOR="#0070c0">Machine-Message,</FONT>
<FONT COLOR="#0070c0">Personal-Information;</FONT></P>
<P STYLE="margin-bottom: 0in"><BR>
</P>"""
soup = BeautifulSoup(html)
print(soup.find("font", color="#ff0000").text)

相关问题 更多 >

    热门问题