Beautiful Soup库入门---Python网络爬虫和信息提取2(北理工mooc)

Beautiful Soup库入门

cmd安装指令:pip install beautifulsoup4

>>> import requests
>>> r = requests.get("https://python123.io/ws/demo.html")
>>> r.text
'<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>'
>>> demo = r.text
>>> from bs4 import BeautifulSoup
>>> soup = BeautifulSoup(demo, "html.parser")
>>> 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>

BeautifulSoup类

BeautifulSoup库的解析器:
在这里插入图片描述
基本元素:
在这里插入图片描述

----------续上
>>> soup.title
<title>This is a python demo page</title>
>>> soup.a    # 返回第一个a标签
<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a>
>>> soup.a.name # 标签a的名字
'a'
>>> soup.a.parent 
<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>
>>> soup.a.parent.name # 标签a的上一级标签的名字
'p'
>>> soup.a.parent.parent.name
'body'
>>> soup.a.attrs # 标签属性,为字典类型
{'href': 'http://www.icourse163.org/course/BIT-268001', 'class': ['py1'], 'id': 'link1'}
>>> soup.a.attrs['class']
['py1']
>>> soup.a.string # 标签内的内容
'Basic Python'
>>> soup.p.string # 实际内部有一个b标签,说明该方法可以跨越多个层次
'The demo python introduces several python courses.'
>>> type(soup.p.string)
<class 'bs4.element.NavigableString'>

遍历:
html基本格式:
在这里插入图片描述
下行遍历:
在这里插入图片描述

--------续上
>>> soup.head
<head><title>This is a python demo page</title></head>
>>> soup.head.contents
[<title>This is a python demo page</title>]
>>> soup.body.contents
['\n', <p class="title"><b>The demo python introduces several python courses.</b></p>, '\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:
<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>, '\n']
>>> len(soup.body.contents)
5
>>> soup.body.contents[1]
<p class="title"><b>The demo python introduces several python courses.</b></p>

在这里插入图片描述
遍历儿孙节点用descendants而不是children
上行遍历:
在这里插入图片描述
在这里插入图片描述
平行遍历:
在这里插入图片描述
平行遍历只能发生在同一个父节点之下。
在这里插入图片描述
prettify()方法用于保持原格式打印html数据。
信息提取方法:

  • 遍历
  • 对文本使用查找函数
    例如:提取所有URL链接
  1. 搜索所有<a>标签
  2. 解析<a>标签格式后提取href后的链接内容
>>> soup = BeautifulSoup(demo,'html.parser')
>>> for link in soup.find_all('a'):
...     print(link.get('href'))
...
http://www.icourse163.org/course/BIT-268001
http://www.icourse163.org/course/BIT-1001870001
>>> soup.find_all('p', 'course')
[<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>]
>>> soup.find_all(id='link1')
[<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a>]
>>> soup.find_all(id='link')
[]
>>> soup.find_all(id=re.compile('link'))
[<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a>, <a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">Advanced Python</a>]

<>.find_all函数
<>.find_all(name, attrs, recursive, string, **kwargs)
返回列表类型,存储查找结果

  • name:查找<a>标签,查找多个参数如同时查找<a>和<b>时,将这个参数用列表形式表示即可如 [‘a’, ‘b’]。使用True时,返回所有标签名字。查找b开头的名字如b和body时,需要使用正则表达式库,import re导入正则表达式库,参数设为re.compile(‘b’),可以查找b开头所有信息
  • attrs:对标签属性值检查字符串,可标注属性检索
  • recursive:是否对子孙全部检索,默认True
  • string:检索<>和</>之间的字符串,必须要完整精确,使用一部分时需要用正则表达式
  • 扩展方法:
    在这里插入图片描述

<tag>(…) 等价于 <tag>.find_all(…)
text属性:tag.text可以返回该标签下的所有文本信息,包括了该标签下子标签的文本信息。和string的不同就在于text可以返回所有文本,但string只能返回这一个标签的文本,当存在多个标签时,string会返回None。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值