Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 获取标签'的内容;在BeautifulSoup 4中作为unicode字符串的s属性_Python_Attributes_Tags_Beautifulsoup - Fatal编程技术网

Python 获取标签'的内容;在BeautifulSoup 4中作为unicode字符串的s属性

Python 获取标签'的内容;在BeautifulSoup 4中作为unicode字符串的s属性,python,attributes,tags,beautifulsoup,Python,Attributes,Tags,Beautifulsoup,根据,可以使用如下代码获取标记属性的值: from bs4 import BeautifulSoup soup = BeautifulSoup('<b class="boldest">Extremely bold</b>') tag = soup.b tag['class'] 但是,当我执行上述代码时,它会输出: ['boldest'] 那么,我有什么遗漏吗?如何以普通unicode字符串的形式获取标记的属性内容 tag['class'][0] 标记中可以有多个

根据,可以使用如下代码获取标记属性的值:

from bs4 import BeautifulSoup

soup = BeautifulSoup('<b class="boldest">Extremely bold</b>')
tag = soup.b

tag['class']
但是,当我执行上述代码时,它会输出:

['boldest']
那么,我有什么遗漏吗?如何以普通unicode字符串的形式获取标记的属性内容

tag['class'][0]

标记中可以有多个类,这就是它返回值列表的原因。如果您确定只有一个类,只需从列表中获取第一个元素。

检查文档中的此部分:

多值属性

HTML4定义了几个可以有多个值的属性。HTML5删除了其中几个,但定义了更多。最常见的多值属性是class(也就是说,一个标记可以有多个CSS类)。其他包括rel、rev、accept字符集、headers和accesskey。Beautiful Soup将多值属性的值显示为列表:

标记['class'][0]
将为您提供字符串

tag['class'][0]