如何使用BeautifulSoup库提取网页标签名称、属性及内容?

2026-06-09 22:535阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计678个文字,预计阅读时间需要3分钟。

如何使用BeautifulSoup库提取网页标签名称、属性及内容?

一、Tag(标签)对象 + 1. Tag对象与XML或HTML原生文档中的tag相同。 + from bs4 import BeautifulSoup + soup=BeautifulSoup('b class=boldest Extremely bold/b', 'lxml') + tag=soup.find('b') + bs4.element.Tag + 2. Tag的Name属性 + 每个tag都有Name属性。

一、Tag(标签)对象

1.Tag对象与XML或HTML原生文档中的tag相同。

from bs4 import BeautifulSoup soup = BeautifulSoup('<b class="boldest">Extremely bold</b>','lxml') tag = soup.b type(tag)

bs4.element.Tag

2.Tag的Name属性

每个tag都有自己的名字,通过.name来获取

tag.name

'b'

tag.name = "blockquote" # 对原始文档进行修改 tag

<blockquote class="boldest">Extremely bold</blockquote>

3.Tag的Attributes属性

获取单个属性

tag['class']

['boldest']

按字典的方式获取全部属性

tag.attrs

{'class': ['boldest']}

添加属性

tag['class'] = 'verybold' tag['id'] = 1 print(tag)

<blockquote class="verybold" id="1">Extremely bold</blockquote>

删除属性

del tag['class'] del tag['id'] tag

<blockquote>Extremely bold</blockquote>

4.Tag的多值属性

多值属性会返回一个列表

css_soup = BeautifulSoup('<p class="body strikeout"></p>','lxml') print(css_soup.p['class'])

['body', 'strikeout']

rel_soup = BeautifulSoup('<p>Back to the <a rel="index">homepage</a></p>','lxml') print(rel_soup.a['rel']) rel_soup.a['rel'] = ['index', 'contents'] print(rel_soup.p)

['index'] <p>Back to the <a rel="index contents">homepage</a></p>

如果转换的文档是XML格式,那么tag中不包含多值属性

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

'body strikeout'

二、可遍历字符串(NavigableString)

1.字符串常被包含在tag内,使用NavigableString类来包装tag中的字符串

from bs4 import BeautifulSoup soup = BeautifulSoup('<b class="boldest">Extremely bold</b>','lxml') tag = soup.b print(tag.string) print(type(tag.string))

Extremely bold <class 'bs4.element.NavigableString'>

2.一个 NavigableString 字符串与Python中的str字符串相同,通过str() 方法可以直接将 NavigableString 对象转换成str字符串

unicode_string = str(tag.string) print(unicode_string) print(type(unicode_string))

Extremely bold <class 'str'>

3.tag中包含的字符串不能编辑,但是可以被替换成其它的字符串,用 replace_with() 方法

tag.string.replace_with("No longer bold") tag

<b class="boldest">No longer bold</b>

三、BeautifulSoup对象 BeautifulSoup 对象表示的是一个文档的全部内容。

大部分时候,可以把它当作 Tag 对象,它支持 遍历文档树 和 搜索文档树 中描述的大部分的方法。

如何使用BeautifulSoup库提取网页标签名称、属性及内容?

四、注释与特殊字符串(Comment)对象

markup = "<b><!--Hey, buddy. Want to buy a used parser?--></b>" soup = BeautifulSoup(markup,'lxml') comment = soup.b.string type(comment)

bs4.element.Comment

Comment 对象是一个特殊类型的 NavigableString 对象

comment

'Hey, buddy. Want to buy a used parser?'

更多关于Python爬虫库BeautifulSoup的使用方法请查看下面的相关链接

本文共计678个文字,预计阅读时间需要3分钟。

如何使用BeautifulSoup库提取网页标签名称、属性及内容?

一、Tag(标签)对象 + 1. Tag对象与XML或HTML原生文档中的tag相同。 + from bs4 import BeautifulSoup + soup=BeautifulSoup('b class=boldest Extremely bold/b', 'lxml') + tag=soup.find('b') + bs4.element.Tag + 2. Tag的Name属性 + 每个tag都有Name属性。

一、Tag(标签)对象

1.Tag对象与XML或HTML原生文档中的tag相同。

from bs4 import BeautifulSoup soup = BeautifulSoup('<b class="boldest">Extremely bold</b>','lxml') tag = soup.b type(tag)

bs4.element.Tag

2.Tag的Name属性

每个tag都有自己的名字,通过.name来获取

tag.name

'b'

tag.name = "blockquote" # 对原始文档进行修改 tag

<blockquote class="boldest">Extremely bold</blockquote>

3.Tag的Attributes属性

获取单个属性

tag['class']

['boldest']

按字典的方式获取全部属性

tag.attrs

{'class': ['boldest']}

添加属性

tag['class'] = 'verybold' tag['id'] = 1 print(tag)

<blockquote class="verybold" id="1">Extremely bold</blockquote>

删除属性

del tag['class'] del tag['id'] tag

<blockquote>Extremely bold</blockquote>

4.Tag的多值属性

多值属性会返回一个列表

css_soup = BeautifulSoup('<p class="body strikeout"></p>','lxml') print(css_soup.p['class'])

['body', 'strikeout']

rel_soup = BeautifulSoup('<p>Back to the <a rel="index">homepage</a></p>','lxml') print(rel_soup.a['rel']) rel_soup.a['rel'] = ['index', 'contents'] print(rel_soup.p)

['index'] <p>Back to the <a rel="index contents">homepage</a></p>

如果转换的文档是XML格式,那么tag中不包含多值属性

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

'body strikeout'

二、可遍历字符串(NavigableString)

1.字符串常被包含在tag内,使用NavigableString类来包装tag中的字符串

from bs4 import BeautifulSoup soup = BeautifulSoup('<b class="boldest">Extremely bold</b>','lxml') tag = soup.b print(tag.string) print(type(tag.string))

Extremely bold <class 'bs4.element.NavigableString'>

2.一个 NavigableString 字符串与Python中的str字符串相同,通过str() 方法可以直接将 NavigableString 对象转换成str字符串

unicode_string = str(tag.string) print(unicode_string) print(type(unicode_string))

Extremely bold <class 'str'>

3.tag中包含的字符串不能编辑,但是可以被替换成其它的字符串,用 replace_with() 方法

tag.string.replace_with("No longer bold") tag

<b class="boldest">No longer bold</b>

三、BeautifulSoup对象 BeautifulSoup 对象表示的是一个文档的全部内容。

大部分时候,可以把它当作 Tag 对象,它支持 遍历文档树 和 搜索文档树 中描述的大部分的方法。

如何使用BeautifulSoup库提取网页标签名称、属性及内容?

四、注释与特殊字符串(Comment)对象

markup = "<b><!--Hey, buddy. Want to buy a used parser?--></b>" soup = BeautifulSoup(markup,'lxml') comment = soup.b.string type(comment)

bs4.element.Comment

Comment 对象是一个特殊类型的 NavigableString 对象

comment

'Hey, buddy. Want to buy a used parser?'

更多关于Python爬虫库BeautifulSoup的使用方法请查看下面的相关链接