爬虫基础2.1|Python学习笔记

Beautiful Soup库的基本元素

  • 理解:Beautiful Soup库是解析、遍历、维护“标签树”的功能库,对应一个HTML、XML文档的全部内容
  1. BS是一个HTML/XML的解析器,主要用于解析和提取HTML/XML数据;
  2. 基于HTML DOM,会载入整个文档,解析整个DOM树,因此时间和内存开销都会大很多,性能低于lxml;
  3. BS用来解析HTML较简单,API非常人性化,支持CSS选择器、Python标准库中的HTML解析器,也支持lxml的XML解析器;
  4. 优点为易上手,但匹配效率不如正则、xpath,推荐正则的使用。
  • 基本元素:
  1. Tag标签:最基本的信息组织单元,分别用<>和</>表明开头和结尾;
  2. Name标签的名字:< p > * < /p >的名字是’p’,格式< tag >.name;
  3. Attributes标签的属性:字典形式组织,格式:< tag >.attrs;
  4. NavigableString标签内非属性字符串:<> * </>中字符串,格式:< tag >.string;
  5. Comment标签内字符串的注释部分,一种特殊的Comment类型;
from bs4 import BeautifulSoup
import requests #抓取页面

r = requests.get('https://python123.io/ws/demo.html')
r
<Response [200]>
demo = r.text#抓取的数据
demo
'<html><head><title>This is a python demo page</title></head>\r\n<body>\r\n<p class="title"><b>The demo python introduces several python courses.</b></p>\r\n<p class="course">Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:\r\n<a href="http://www.icourse163.org/course/BIT-268001" class="py1" id="link1">Basic Python</a> and <a href="http://www.icourse163.org/course/BIT-1001870001" class="py2" id="link2">Advanced Python</a>.</p>\r\n</body></html>'
#解析HTML页面
#创建Beautiful Soup对象
soup = BeautifulSoup(demo,'html.parser')
#抓取的页面数据;bs4的解析器
soup
<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>
print(soup.prettify)#有层次感的输出解析后的HTML页面
<bound method Tag.prettify of <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>>
A.标签(用soup.< tag >访问获得):
  • 当HTML文档中存在多个相同< tag >对应内容时,soup.< tag >返回第一个
soup.a#访问标签a
<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a>
soup.title
<title>This is a python demo page</title>
B.标签的名字:每个< tag >都有自己的名字,通过soup.< tag >.name获取,字符串类型
soup.a.name
'a'
soup.a.parent.name#返回<a>标签父辈标签名字
'p'
soup.p.parent.name
'body'
C.标签属性,一个< tag >可以有0或多个属性,字典类型,soup.< tag >.attrs
tag = soup.a
print(tag.attrs)
print(tag.attrs['class'])
print(type(tag.attrs))
{'href': 'http://www.icourse163.org/course/BIT-268001', 'class': ['py1'], 'id': 'link1'}
['py1']
<class 'dict'>
D.Attributes:标签内非属性字符串,格式:soup.< tag >.string,NavigableString可以跨越多个层次
print(soup.a.string)
print(type(soup.a.string))
Basic Python
<class 'bs4.element.NavigableString'>
E.NavigableString:标签内字符串的注释部分,Comment是一种特殊类型(有–>)
print(type(soup.p.string))
<class 'bs4.element.NavigableString'>
F.prettify()为HTML文本<>及其内容增加’\n’,更加有层次感的输出

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

print(soup.prettify())
<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>
print(soup)#对比直接输出
<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>
print(soup.a.prettify())
print('对比无prettify()输出: \n',soup.a)
<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">
 Basic Python
</a>

对比无prettify()输出: 
 <a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a>
G.BS4库将任何HTML输入都变成utf-8编码
  • python 3.x默认支持编码是utf-8,解析无障碍
newsoup = BeautifulSoup('<a>中文</a>','html.parser')
print(newsoup.prettify())
<a>
 中文
</a>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值