2020-07-25→Python爬虫课第八节_BeautifulSoup4(上)

一、bs4简介

1.1 基本概念

  • Beautiful Soup是⼀个可以从HTML或XML⽂件中提取数据的⽹⻚信息提取库。(名称取自爱丽丝梦游仙境)其必需依靠lxml模块!

1.2 源码分析

  • github下载源码
  • 安装
    pip install lxml
    pip install bs4

二、bs4的使用

2.1 快速开始

  • 经典代码
html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>

<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>

<p class="story">...</p>
"""

“漂亮打印输出”:

image.png

bs4的一些基本使用方法如下图:
image.png

如果要查找全部,则用find_all('字符串')
eg.
image.png

查找相关数据:


image.png
image.png

2.2 bs4的对象种类(结合经典代码)

  • tag : 标签
    eg.


    image.png
  • NavigableString : 可导航的字符串
    eg.


    image.png
  • BeautifulSoup : bs对象

  • Comment : 注释
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
禁止转载,如需转载请通过简信或评论联系作者。