【问题标题】:How to get multiple class names in BeautifulSoup4如何在 BeautifulSoup4 中获取多个类名
【发布时间】:2021-08-13 04:26:08
【问题描述】:

我在python中遇到了bs4的问题,在下面的代码中,我想从标签中知道第二个类名

HTML 代码:

<span class="type Manhwa"></span>

所以我写了这个

contendorTipo = BeautifulSoup('<span class="type Manhwa"></span>', 'html.parser')
tipo = contendorTipo.p['class']

在 bs4 文档示例 (https://www.crummy.com/software/BeautifulSoup/bs4/doc/#multi-valued-attributes) 中是这样写的

所以我希望控制台打印一个包含 2 个值(类型和 Manhwa)的数组或包含两个类的字符串,但我在控制台中看到以下错误

TypeError: 'NoneType' object is not subscriptable

请帮帮我! x'd

【问题讨论】:

  • 您的代码中有span 标签,在您的第二行中使用了p 标签,那么它将如何工作!

标签: python beautifulsoup


【解决方案1】:

你应该使用span标签而不是p标签来获得classes

from bs4 import BeautifulSoup
contendorTipo = BeautifulSoup('<span class="type Manhwa"></span>', 'html.parser')
classes = contendorTipo.span['class']

classes

输出:

['type', 'Manhwa']

【讨论】:

    猜你喜欢
    • 2020-11-04
    • 2016-03-29
    • 1970-01-01
    • 2021-05-16
    • 1970-01-01
    • 2015-07-04
    • 2019-11-11
    • 2022-01-09
    • 2020-11-10
    相关资源
    最近更新 更多