python爬虫(三)Beautiful Soup库使用

一、Beautiful Soup库安装

window或Linux均可使用命令 pip install beautifulsoup4

二、HTML文档示例

<!--http://python123.io/ws/demo.html-->
<html>
 <head>
  <title>
   This is a python demo page
  </title>
 </head>
 <body>
  <p class="title">
   <b>
    The demo python introduces several python courses.
   </b>
  </p>
  <p class="course">
   Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:
   <a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">
    Basic Python
   </a>
   and
   <a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">
    Advanced Python
   </a>
   .
  </p>
 </body>
</html>

三、BeautifulSoup类基本元素

              基本元素                                                说明
Tag标签,最基本的信息组织单元,分别用<>和</>标明开头和结尾
Name标签的名字, <>...</>的名字是'p' ,格式: <tag> . name
Attributes标签的属性,字典形式组织,格式: <tag>. attrs
NavigableString标签内非属性字符串, <...</>中字符串,格式: <tag>. string
Comment标签内字符串的注释部分,一种特殊的Comment类型

 

>>> import requests
>>> from bs4 import BeautifulSoup
>>> r = requests.get("http://python123.io/ws/demo.html")
>>> demo = r.text
>>> soup = BeautifulSoup(demo,"html.parser")
>>> tag = soup.a
>>> tag
<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a>
>>> soup.a.name
'a'
>>> soup.a.attrs
{'href': 'http://www.icourse163.org/course/BIT-268001', 'class': ['py1'], 'id': 'link1'}
>>> soup.a.string
'Basic Python'
>>> newsoup = BeautifulSoup("<b><!--this is a comment--></b><p>123</p>","html.parser")
>>> newsoup.b.string
'this is a comment'
>>> type(newsoup.b.string)
<class 'bs4.element.Comment'>

四、标签树的遍历

BeautifulSoup类型是标签树的根节点

1.下行遍历

              属性                                                         说明
. contents子节点的列表,将<tag>所有儿子节点存入列表
. children子节点的迭代类型,与. contents类似,用于循环遍历儿子节点
.descendants子孙节点的迭代类型,包含所有子孙节点,用于循环遍历

 

>>> soup.head.contents
[<title>This is a python demo page</title>]
>>> soup.body.contents[1]
<p class="title"><b>The demo python introduces several python courses.</b></p>
>>> for child in soup.body.children:
	print(child)
>>> for child in soup.body.descendants:
	print(child)

2.上行遍历

            属性                                                  说明
.parent节点的父亲标签
.parents节点的先辈标签的迭代类型,用于迭代遍历

 

>>> for parent in soup.a.parents:
	if parent is None:
		print(parent)
	else:
		print(parent.name)

p
body
html
[document]

3.平行遍历

            属性                                                       说明
.next_ sibling返回按照HTML文本顺序的下一个平行节点标签
.previous_ sibling返回按照HTML文本顺序的.上一个平行节点标签
.next_ siblings迭代类型,返回按照HTML文本顺序的后续所有平行节点标签
.previous_ siblings迭代类型,返回按照HTML文本顺序的前续所有平行节点标签
>>> soup.a.next_sibling
' and '
>>> soup.a.previous_sibling
'Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:\r\n'
>>> for sibling in soup.a.next_siblings:
	print(sibling)

 and 
<a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">Advanced Python</a>
.
>>> for sibling in soup.a.previous_siblings:
	print(sibling)

Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:

五、格式化输出

.prettify()方法,可用于标签,tag.prettify()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值