python - 如何在 BeautifulSoup4 中获取多个类名
问题描述
我在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
解决方案
而不是p
标签,你应该span
在你的标签中使用classes
from bs4 import BeautifulSoup
contendorTipo = BeautifulSoup('<span class="type Manhwa"></span>', 'html.parser')
classes = contendorTipo.span['class']
classes
输出:
['type', 'Manhwa']
推荐阅读
- oracle - 创建具有外键约束的表字段
- appium - WebView 在 Appium Inspector 中被视为原生,无法切换到 webview 来查找元素
- css - 在 a 中使用 CSS
- c# - 如何使用 C# 创建一个对象以统一在两个位置之间移动,为什么我的代码不起作用?
- c# - 使用带有嵌入字体的 iText 提取文本
- pandas - Pandas 跨时间窗口聚合用户日志
- xpages - XPages REST viewJsonService
- c# - 无法读取请求正文 - 身份服务器回调 url
- flutter - 在颤动的模态底页中实现WebView
- java - IntelliJ 插件开发:上下文项在 IDEA 中显示,但在 WebStorm 中不显示