【问题标题】:Retrieve the attribute value from the HTML tag using python beautiful soup使用python美汤从HTML标签中检索属性值
【发布时间】:2020-12-22 13:52:04
【问题描述】:

我正在尝试仅从 HTML 页面的所有 FONT 标记中检索属性值。 HTML 内容(对于众多 FONT 标签之一)如下:

<tr>
    <td align="left" valign="top">&#546;</td>
        <td>
            <a href="space.xml" target="Dest">
                <FONT COLOR="#0000FF">Start</FONT>
            </a>
        </td>
</tr>

我想检索 FONT COLOR 值并将其添加到列表中,发布我想检查该值是#0000FF 还是#FF0000。但是当试图只检索值时,它会获取整个标签。我的代码 sn-p 如下所示:

trs = soup_file.select("[COLOR]")
print(trs)

任何帮助将不胜感激。 提前致谢

【问题讨论】:

  • 问题解决了吗?如果是这样,请将任何答案标记为已接受。

标签: html python-3.x beautifulsoup


【解决方案1】:

你可以这样做:

soup_file = BeautifulSoup(above_html_string, "html.parser")
color = soup_file.find("font").attrs["color"]

会给你

'#0000FF'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-16
    • 2020-07-16
    • 1970-01-01
    • 1970-01-01
    • 2014-04-16
    • 2020-06-25
    • 2018-07-06
    • 1970-01-01
    相关资源
    最近更新 更多