Beautiful Soup 对象种类

Beautiful Soup 对象种类

当我们将一个html文档或字符串传递给beautifulsoup构造器时,beautifulsoup基本上将复杂的html页面转换为不同的python对象。下面我们将讨论四种主要的对象:

  • 标签(Tag)

  • 可导航字符串(NavigableString)

  • BeautifulSoup

  • 注释(Comments)

标签对象

HTML标签用于定义各种类型的内容。在BeautifulSoup中,标签对象对应于实际页面或文档中的HTML或XML标签。

>>> from bs4 import BeautifulSoup
>>> soup = BeautifulSoup('<b class="boldest">TutorialsPoint</b>')
>>> tag = soup.html
>>> type(tag)
<class 'bs4.element.Tag'>

标签包含许多属性和方法,标签的两个重要特征是其名称和属性。

名称(tag.name)

每个标签都包含一个名称,可以通过后缀‘.name’访问。tag.name将返回标签的类型。

>>> tag.name
'html'

然而,如果我们更改标签名,那么BeautifulSoup生成的HTML标记也将反映出这一变化。

>>> tag.name = "Strong"
>>> tag
<Strong><body><b class="boldest">TutorialsPoint</b></body></Strong>
>>> tag.name
'Strong'

属性(tag.attrs)

标签对象可以拥有任意数量的属性。标签有一个名为‘class’的属性,其值为“boldest”。任何不是标签的东西,基本上都是一个属性,必须包含一个值。您可以通过访问键(例如在上面的示例中访问“class”)或直接访问“.attrs”来访问属性。

>>> tutorialsP = BeautifulSoup("<div class='tutorialsP'></div>",'lxml')
>>> tag2 = tutorialsP.div
>>> tag2['class']
['tutorialsP']

我们可以对标签的属性进行各种修改(添加/移除/修改)。

>>> tag2['class'] = 'Online-Learning'
>>> tag2['style'] = '2007'
>>>
>>> tag2
<div class="Online-Learning" style="2007"></div>
>>> del tag2['style']
>>> tag2
<div class="Online-Learning"></div>
>>> del tag['class']
>>> tag
<b SecondAttribute="2">TutorialsPoint</b>
>>>
>>> del tag['SecondAttribute']
>>> tag
</b>
>>> tag2['class']
'Online-Learning'
>>> tag2['style']
KeyError: 'style'

多值属性

一些HTML5属性可以具有多个值。最常用的是class属性,它可以具有多个CSS值。其他属性包括’rel’、’rev’、’headers’、’accesskey’和’accept-charset’。在beautiful soup中,多值属性显示为列表。

>>> from bs4 import BeautifulSoup
>>>
>>> css_soup = BeautifulSoup('<p class="body"></p>')
>>> css_soup.p['class']
['body']
>>>
>>> css_soup = BeautifulSoup('<p class="body bold"></p>')
>>> css_soup.p['class']
['body', 'bold']

然而,如果任何属性包含多个值但不符合任何HTML标准的多值属性,Beautiful Soup将保留该属性不变−

>>> id_soup = BeautifulSoup('<p id="body bold"></p>')
>>> id_soup.p['id']
'body bold'
>>> type(id_soup.p['id'])
<class 'str'>

通过将标签转换为字符串,您可以合并多个属性值。

>>> rel_soup = BeautifulSoup("<p> tutorialspoint Main <a rel='Index'> Page</a></p>")
>>> rel_soup.a['rel']
['Index']
>>> rel_soup.a['rel'] = ['Index', ' Online Library, Its all Free']
>>> print(rel_soup.p)
<p> tutorialspoint Main <a rel="Index Online Library, Its all Free"> Page</a></p>

通过使用’get_attribute_list’,您得到的值始终是一个列表、字符串,无论它是多值还是单值。

id_soup.p.get_attribute_list(‘id’)

然而,如果您将文档解析为“xml”,则没有多值属性 –

>>> xml_soup = BeautifulSoup('<p class="body bold"></p>', 'xml')
>>> xml_soup.p['class']
'body bold'

可导航字符串

可导航字符串对象用于表示标签的内容。要访问内容,请使用带有标签的“.string”。

>>> from bs4 import BeautifulSoup
>>> soup = BeautifulSoup("<h2 id='message'>Hello, Tutorialspoint!</h2>")
>>>
>>> soup.string
'Hello, Tutorialspoint!'
>>> type(soup.string)
>

你可以用另一个字符串替换原字符串,但不能编辑原有字符串。

>>> soup = BeautifulSoup("<h2 id='message'>Hello, Tutorialspoint!</h2>")
>>> soup.string.replace_with("Online Learning!")
'Hello, Tutorialspoint!'
>>> soup.string
'Online Learning!'
>>> soup
<html><body><h2 id="message">Online Learning!</h2></body></html>

BeautifulSoup

BeautifulSoup是当我们尝试抓取一个网络资源时创建的对象。因此,它是我们试图抓取的完整文档。大多数情况下,它被视为标签对象。

>>> from bs4 import BeautifulSoup
>>> soup = BeautifulSoup("<h2 id='message'>Hello, Tutorialspoint!</h2>")
>>> type(soup)
<class 'bs4.BeautifulSoup'>
>>> soup.name
'[document]'

注释

注释对象说明了网页文档中的注释部分。它只是一种特殊类型的可导航字符串。

>>> soup = BeautifulSoup('<p><!-- Everything inside it is COMMENTS --></p>')
>>> comment = soup.p.string
>>> type(comment)
<class 'bs4.element.Comment'>
>>> type(comment)
<class 'bs4.element.Comment'>
>>> print(soup.p.prettify())
<p>
<!-- Everything inside it is COMMENTS -->
</p>

可导航字符串对象

导航字符串对象用于表示标签内的文本,而不是标签本身。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程