第14.10节 Python中使用BeautifulSoup解析http报文:html标签相关属性的访问

29 篇文章 23 订阅

一、 引言
在《第14.8节 Python中使用BeautifulSoup加载HTML报文》中介绍使用BeautifulSoup的安装、导入和创建对象的过程,本节介绍导入后利用BeautifulSoup对象访问相关标签数据。
本节案例中介绍处理的c:\temp\s1.html文件内容如下:

<!DOCTYPE html>
<html lang="zh-CN">
<head>
<style type="text/css">	.textline{color:blue;}</style>
<link href="https://blog.csdn.net/LaoYuanPython/article/details/95360624" rel="canonical"/>
<title>BeautifulSoups使用方法 - 老猿Python - CSDN博客 </title></head>
<body> 	
  <h1>老猿Python</h1>
	<div><p class="textline" name="line1"> 老猿Python首行</p></div>
	<div>
	<h2>老猿Python第二行<a href="https://blog.csdn.net/LaoYuanPython" /> </h2>
	<h3><b>老猿Python第三行</b><a href="https://blog.csdn.net/LaoYuanPython" /> </h3>
  </div>	
</body></html>

创建soup对象的代码如下:

>>> from bs4 import BeautifulSoup
>>> def getsoup():
    fp = open(r'c:\temp\s1.html',encoding='utf-8')
    soup = BeautifulSoup(fp, 'lxml')
    fp.close()
    print(soup)
    return soup

>>> soup=getsoup()

二、 访问标签及其属性数据
通过BeautifulSoup对象可以访问标签对应的html元素、并进一步访问标签的名字、属性、html元素标签对中的内容。

  1. 通过Tag标签获取HTML元素内容
    Tag就是 HTML 中的一个个标签,用 BeautifulSoup 可以很方便地获取 T标签,通过标签名可以获取HTML报文中对应标签的第一个记录。注意标签识别时对大小写敏感(标签应该都是小写)。
    如上面读取文件解析报文构建soap对象后,访问相关的标签的结果:
>>> soup.title
<title>BeautifulSoups使用方法 - 老猿Python - CSDN博客 </title>
>>> soup.p
<p class="textline" name="line1"> 老猿Python首行</p>
>>> soup.link
<link href="https://blog.csdn.net/LaoYuanPython/article/details/95360624" rel="canonical"/>

  1. 通过标签获取标签属性
    通过BeautifulSoup对象的标签名的attrs属性可以访问标签的所有属性,返回的属性为一个字典,如果通过标签名加属性名的方法可以访问属性的值。如:
>>> soup.link.attrs
{'href': 'https://blog.csdn.net/LaoYuanPython/article/details/95360624', 'rel': ['canonical']}
>>> soup.link['rel']
['canonical']
>>> soup.link.name
'link'
>>>

除了读取相关数据外,还可以通过赋值进行内存数据的修改,如:

>>> soup.title = '老猿Python'

注意:
 上述通过“BeautifulSoup对象.标签”去访问的数据都是html报文中第一个匹配的标签的内容;
 可以通过“BeautifulSoup对象.标签.attrs”访问标签的所有属性数据,如soup.link.attrs,通过“soup.标签[“属性名”]”访问具体属性的值,如soup.link[‘rel’]。

  1. 无嵌套情况下通过标签获取标签内的文字内容
    当通过标签获取到标签内容后,可以通过标签内容的string属性获取标签内的文字。由于string属性返回内容的类型为bs4.element.NavigableString,所以称获取的文本为NavigableString,如:
>>> soup.title.string
'BeautifulSoups使用方法 - 老猿Python - CSDN博客 '
>>> soup.link
<link href="https://blog.csdn.net/LaoYuanPython/article/details/95360624" rel="canonical"/>
>>> soup.link.string
>>> soup.h1.string
'老猿Python'
>>>

上面soup.link.string没有数据,是因为link只有标签数据而标签外无内容。我们来看看数据类型:

>>> type( soup.title.string)
<class 'bs4.element.NavigableString'>
>>> soup.title.string[0:10]
'BeautifulS'
>>>
  1. 标签嵌套情况下通过父标签获取标签内的文字内容,这又分为三种情况:
    1)如果标签里面只有唯一的一个标签无其他内容,那么string会返回最里面嵌套标签对应的内容。如:
>>> soup.div
<div><p class="textline" name="line1"> 老猿Python首行</p></div>
>>> soup.div.string
' 老猿Python首行'
>>> soup.div.p.string
' 老猿Python首行'
>>>

2)如果tag包含了1个子节点且本身标签内还有文本内容,无法确认该返回哪个文本内容,因此模块给string 的值为 None。如:

>>> soup.h2
<h2>老猿Python第二行<a href="https://blog.csdn.net/LaoYuanPython"></a> </h2>
>>> soup.h2.string
>>> soup.h2.a
<a href="https://blog.csdn.net/LaoYuanPython"></a>
>>>

3)如果tag包含了多个子节点,string 的输出结果也是 None。

>>> soup.h3
<h3><b>老猿Python第三行</b><a href="https://blog.csdn.net/LaoYuanPython"></a> </h3>
>>> soup.h3.string
>>> soup.h3.b
<b>老猿Python第三行</b>
>>> soup.h3.b.string
'老猿Python第三行'
>>>

三、 嵌套标签节点访问

  1. 通过标签的contents属性,可以访问其下嵌套的所有下级HTML元素,这些该标签下的子标签对应的HTML元素放到一个content 指向的列表中。如:
>>> print(soup.body.contents)
['\n', <h1>老猿Python</h1>, '\n', <div><p class="textline" name="line1"> 老猿Python首行</p></div>, '\n', <div>
<h2>老猿Python第二行<a href="https://blog.csdn.net/LaoYuanPython"></a> </h2>
<h3><b>老猿Python第三行</b><a href="https://blog.csdn.net/LaoYuanPython"></a> </h3>
</div>, '\n']
>>>

注意换行符和空行都会作为一个列表元素返回。
2. 通过标签的children属性也可以访问标签下嵌套的所有HTML元素,只是其类型不是列表,而是一个迭代器。如:

>>> for i in soup.body.children:print(i,end=', ')


, <h1>老猿Python</h1>, 
, <div><p class="textline" name="line1"> 老猿Python首行</p></div>, 
, <div>
<h2>老猿Python第二行<a href="https://blog.csdn.net/LaoYuanPython"></a> </h2>
<h3><b>老猿Python第三行</b><a href="https://blog.csdn.net/LaoYuanPython"></a> </h3>
</div>, 
, 
>>>
>>> type(soup.body.children)
<class 'list_iterator'>
>>>

从上述输出可以看到children与contents还是有换行符和空行。

  1. 访问嵌套标签的所有子孙节点
    escendants 属性可以对标签其下所有层次的节点进行访问,而 children只能访问直接子节点,注意descendants返回的是一个生成器。
>>> type(soup.body.descendants)
<class 'generator'>
>>> for i in soup.body.descendants:print(i,end=', ')


, <h1>老猿Python</h1>, 老猿Python, 
, <div><p class="textline" name="line1"> 老猿Python首行</p></div>, <p class="textline" name="line1"> 老猿Python首行</p>,  老猿Python首行, 
, <div>
<h2>老猿Python第二行<a href="https://blog.csdn.net/LaoYuanPython"></a> </h2>
<h3><b>老猿Python第三行</b><a href="https://blog.csdn.net/LaoYuanPython"></a> </h3>
</div>, 
, <h2>老猿Python第二行<a href="https://blog.csdn.net/LaoYuanPython"></a> </h2>, 老猿Python第二行, <a href="https://blog.csdn.net/LaoYuanPython"></a>,  , 
, <h3><b>老猿Python第三行</b><a href="https://blog.csdn.net/LaoYuanPython"></a> </h3>, <b>老猿Python第三行</b>, 老猿Python第三行, <a href="https://blog.csdn.net/LaoYuanPython"></a>,  , 
, 
, 
>>>
  1. 访问标签的父节点
    可以通过标签的parent属性访问其直接父节点,如:
>>> soup.b.parent
<h3><b>老猿Python第三行</b><a href="https://blog.csdn.net/LaoYuanPython"></a> </h3>
>>>
  1. 访问标签的所有祖先节点
    通过标签的parents属性可以访问其所有父节点及祖先节点,该值为一个生成器,如:
>>> type(soup.b.parents)
<class 'generator'>
>>> for i in soup.b.parents:print(i.name,i.string)

h3 None
div None
body None
html None
[document] None
>>>
  1. 访问标签的同父节点下的同级兄弟节点
    next_sibling 属性为节点的下一个兄弟节点,previous_sibling 属性为节点的上一个兄弟节点,如果节点不存在,则返回 None,由于空白或者换行也可以被视作一个节点,所以得到的结果可能是空白或者换行。
>>> soup.b.next_sibling
<a href="https://blog.csdn.net/LaoYuanPython"></a>
>>> soup.b.pre_sibling
>>>
  1. 访问标签的同父节点下的所有同级兄弟节点
    通过next_siblings 和 previous_siblings 属性可以对当前节点的所有兄弟节点迭代访问,如:
>>> for i in soup.h2.next_siblings:print(i)

<h3><b>老猿Python第三行</b><a href="https://blog.csdn.net/LaoYuanPython"></a> </h3>

>>> for i in soup.h3.previous_siblings:print(i)

<h2>老猿Python第二行<a href="https://blog.csdn.net/LaoYuanPython"></a> </h2>

>>>
  1. 访问标签的前后节点
    通过next_element和previous_element可以访问标签的前后元素,这里的前后是指html文档中当前标签的前一个元素和后一个元素,不论标签层级,只是字符串的位置前后。并且不一定是标签,只要是独立含义的部分。如:
>>> soup.b.next_element
'老猿Python第三行'
>>> soup.b.previous_element
<h3><b>老猿Python第三行</b><a href="https://blog.csdn.net/LaoYuanPython"></a> </h3>
>>>
  1. 访问标签的所有前后节点
    通过 next_elements 和 previous_elements 的生成器就可以访问当前标签所有前面和后面的html文档解析元素。如:
>>> for n in soup.a.next_elements:
	if n!=None and n.name!=None:print(n.name)

h3
b
a
>>>  for n in soup.head.previous_elements:print(n.name)
None
html
None
>>>

注意部分元素可能是空行或换行符。

  1. 访问标签的所有内容
    使用标签的contents属性可以访问该标签下的所有元素,如:
>>> soup.div.contents
['\n', <h1 class="t1" id="l1" name="line1">老猿Python第1行</h1>, '\n', <h2 class="t2" id="l2" name="line2">老猿Python第2行</h2>, '\n', <h3 class="t3" id="l3" name="line3">老猿Python第3行</h3>, '\n', <div>
<h1 class="t1" id="l4" name="line4">LaoYuanPython第1行</h1>
<h2 class="t2" id="l5" name="line5">LaoYuanPython第2行</h2>
<h3 class="t3" id="l6" name="line6">LaoYuanPython第3行</h3>
</div>, '\n']
>>> soup.h1.contents
['老猿Python第1行']
>>>

四、 访问对象的所有内容

  1. 通过对象的strings属性迭代访问除标签外的所有内容,包括空行、空白行,如:
>>> for i in soup.head.strings:print(i)


	.textline{color:blue;}




BeautifulSoups使用方法 - 老猿Python - CSDN博客 
>>> 
  1. 通过对象的stripped_strings属性迭代访问所有内容,去除空行、空白行,如:
>>> for i in soup.head.stripped_strings:print(i)
.textline{color:blue;}
BeautifulSoups使用方法 - 老猿Python - CSDN博客
>>>

本节介绍了BeautifulSoup对象的主要属性,通过这些属性可以访问特定标签和内容。

老猿Python,跟老猿学Python!
博客地址:https://blog.csdn.net/LaoYuanPython

老猿Python博客文章目录:https://blog.csdn.net/LaoYuanPython/article/details/98245036
请大家多多支持,点赞、评论和加关注!谢谢!

  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LaoYuanPython

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值