zoukankan      html  css  js  c++  java
  • python爬虫之BeautifulSoup

    Beautiful Soup,字面意思是美好的汤,是一个用于解析HTML文件的Python库

    windows下载和安装

    在Windows下面如何安装Beautiful Soup:
    2.下载完成之后需要解压缩,假设放到D:/python下。
    3.运行cmd,切换到D:/python/beautifulsoup4-4.1.3/目录下(根据自己解压缩后的目录和下载的版本号修改),
    cd /d D:/python/beautifulsoup4-4.1.3
    4.运行命令:
    setup.py build
    setup.py install
    5.在IDE下from bs4 import BeautifulSoup,没有报错说明安装成功。

    装汤——Making the Soup

    首先要把待解析的HTML装入BeautifulSoup。BeautifulSoup可以接受文件句柄或是字符串作为输入:

    方式一:
    from bs4 import BeautifulSoup fp = open("index.html") soup1 = BeautifulSoup(fp) #soup2 = BeautifulSoup("<html>data</html>")
     
    方式二:
    html = """
    <html><head><title>The Dormouse's story</title></head>
    <body>
    <p class="title" name="dromouse"><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>
    """
    soup = BeautifulSoup(html)
    soup = BeautifulSoup(html)
    print soup.prettify()
    这里字符串用三个引号(单引号,双引号)代表保留字符串里原有的格式。
    三引号让程序员从引号和特殊字符串的泥潭里面解脱出来,自始至终保持一小块字符串的格式是所谓的WYSIWYG(所见即所得)格式的
     

    汤料——Soup中的对象

    这里有四个对象tag,NavigableString(用来输出标签里的文字部分)

    ,

    (1)Tag

    <html><head><title>The Dormouse's story</title></head>
    <p class="title" name="dromouse"><b>The Dormouse's story</b></p>
    <a href="http://example.com/elsie" class="sister" id="link1"><!-- Elsie --></a>,

    这里的head,title,p,a都是标签tag
    print soup.p ,'
    ',soup.a ,'
    ',soup.head,'
    ', soup.title,'
    '

    输出结果:

    <p class="title" name="dromouse"><b>The Dormouse's story</b></p>
    <a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>
    <head><title>The Dormouse's story</title></head>
    <title>The Dormouse's story</title>

    我们可以利用 soup加标签名轻松地获取这些标签的内容,是不是感觉比正则表达式方便多了?不过有一点是,它查找的是在所有内容中的第一个符合要求的标签

    我们可以验证一下这些对象的类型

    print '
    ',type(soup.a),'
    ',type(soup.p)

    输出结果:

    <class 'bs4.element.Tag'>
    <class 'bs4.element.Tag'>

    对于 Tag,它有两个重要的属性,是 name 和 attrs,

    soup 对象本身比较特殊,它的 name 即为 [document],对于其他内部标签,输出的值便为标签本身的名称。

    print soup.name
    print soup.p.name,soup.head.name

    输出结果:

    [document]
    p head

    attr:

    print soup.p['name']输出标签p里的属性name的值

    (2)NavigableString

    print soup.p.string
    输出结果:

    The Dormouse's story

    (3)BeautifulSoup

    BeautifulSoup 对象表示的是一个文档的全部内容.大部分时候,可以把它当作 Tag 对象,是一个特殊的 Tag,我们可以分别获取它的类型,名称,以及属性

    (4)Comment

    Comment 对象是一个特殊类型的 NavigableString 对象,其实输出的内容仍然不包括注释符号

    比如标签a<a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a> 

    这里就包括了注释符号<!-- -->

    a 标签里的内容实际上是注释,但是如果我们利用 .string 来输出它的内容,我们发现它已经把注释符号去掉了,所以这可能会给我们带来不必要的麻烦。

    另外我们打印输出下它的类型,发现它是一个 Comment 类型,所以,我们在使用前最好做一下判断,判断代码如下

    上面的代码中,我们首先判断了它的类型,是否为 Comment 类型,然后再进行其他操作,如打印输出。

    未完待续~

  • 相关阅读:
    004_Linux命令之查找与检索
    Linux passwd文件释义
    等保2.0涉及的PostgreSQL
    等保2.0测评:VMware ESXI
    等保2.0测评:Redis 数据库配置
    等保2.0测评之Nginx 中间件
    netcat使用方法
    Zookeeper的功能以及工作原理
    Nginx三大功能
    jellyfin错误 播放错误 该客户端与媒体不兼容,服务器未发送兼容的媒体格式 的解决办法
  • 原文地址:https://www.cnblogs.com/ldphoebe/p/5571354.html
Copyright © 2011-2022 走看看