最新公告
  • 欢迎您光临起源地模板网,本站秉承服务宗旨 履行“站长”责任,销售只是起点 服务永无止境!立即加入钻石VIP
  • 详解BS4库的安装与使用

    正文概述    2020-02-17   248

    详解BS4库的安装与使用

    Beautiful Soup 库一般被称为bs4库,支持Python3,是我们写爬虫非常好的第三方库。因用起来十分的简便流畅。所以也被人叫做“美味汤”。目前bs4库的最新版本是4.60。下文会介绍该库的最基本的使用,具体详细的细节还是要看:[官方文档](Beautiful Soup Documentation)

    bs4库的安装

    Python的强大之处就在于他作为一个开源的语言,有着许多的开发者为之开发第三方库,这样我们开发者在想要实现某一个功能的时候,只要专心实现特定的功能,其他细节与基础的部分都可以交给库来做。bs4库 就是我们写爬虫强有力的帮手。

    安装的方式非常简单:我们用pip工具在命令行里进行安装

    pip install beautifulsoup4

    接着我们看一下是否成功安装了bs4库

    pip list

    这样我们就成功安装了 bs4 库

    详解BS4库的安装与使用

    bs4库的简单使用

    这里我们先简单的讲解一下bs4库的使用,

    暂时不去考虑如何从web上抓取网页,

    假设我们需要爬取的html是如下这么一段:

    下面的一段HTML代码将作为例子被多次用到.这是 爱丽丝梦游仙境的 的一段内容(以后内容中简称为 爱丽丝 的文档):
    
    <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
    http://example.com/elsie" class="sister" id="link1">Elsie,
    http://example.com/lacie" class="sister" id="link2">Lacie and
    http://example.com/tillie" class="sister" id="link3">Tillie;
    and they lived at the bottom of a well.</p>
        
    <p class="story">...</p>
    </html>

    下面我们开始用bs4库解析这一段html网页代码。

    #导入bs4模块
    from bs4 import BeautifulSoup
    #做一个美味汤
    soup = BeautifulSoup(html,'html.parser')
    #输出结果
    print(soup.prettify())
        
    '''
    OUT:
        
    # <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 class="sister" href="http://example.com/elsie" id="link1">
    #     Elsie
    #    </a>
    #    ,
    #    <a class="sister" href="http://example.com/lacie" id="link2">
    #     Lacie
    #    </a>
    #    and
    #    <a class="sister" href="http://example.com/tillie" id="link2">
    #     Tillie
    #    </a>
    #    ; and they lived at the bottom of a well.
    #   </p>
    #   <p class="story">
    #    ...
    #   </p>
    #  </body>
    # </html>
    '''

    可以看到bs4库将网页文件变成了一个soup的类型,

    事实上,bs4库 是解析、遍历、维护、“标签树“的功能库。

    通俗一点说就是: bs4库把html源代码重新进行了格式化,

    从而方便我们对其中的节点、标签、属性等进行操作。

    下面是几个简单的浏览结构化数据的方式 :

    请仔细观察最前面的html文件

    # 找到文档的title
    soup.title
    # <title>The Dormouse's story</title>
        
    #title的name值
    soup.title.name
    # u'title'
        
    #title中的字符串String
    soup.title.string
    # u'The Dormouse's story'
        
    #title的父亲节点的name属性
    soup.title.parent.name
    # u'head'
        
    #文档的第一个找到的段落
    soup.p
    # <p class="title"><b>The Dormouse's story</b></p>
        
    #找到的p的class属性值
    soup.p['class']
    # u'title'
        
    #找到a标签
    soup.a
    # http://example.com/elsie" id="link1">Elsie
        
    #找到所有的a标签
    soup.find_all('a')
    # [http://example.com/elsie" id="link1">Elsie,
    #  http://example.com/lacie" id="link2">Lacie,
    #  http://example.com/tillie" id="link3">Tillie]
        
    #找到id值等于3的a标签
    soup.find(id="link3")
    # http://example.com/tillie" id="link3">Tillie

    通过上面的例子 我们知道bs4库是这样理解一个html源文件的:

    首先 把html源文件转换为soup类型

    接着 从中通过特定的方式抓取内容

    更高级点的用法?

    从文档中找到所有<a>标签的链接:

    #发现了没有,find_all方法返回的是一个可以迭代的列表for link in soup.find_all('a'):
        print(link.get('href'))    # http://example.com/elsie
        # http://example.com/lacie
        # http://example.com/tillie

    从文档中获取所有文字内容:

    #我们可以通过get_text 方法 快速得到源文件中的所有text内容。
    print(soup.get_text())
    # The Dormouse's story
    #
    # The Dormouse's story
    #
    # Once upon a time there were three little sisters; and their names were
    # Elsie,
    # Lacie and
    # Tillie;
    # and they lived at the bottom of a well.
    #
    # ...

    起源地下载网 » 详解BS4库的安装与使用

    常见问题FAQ

    免费下载或者VIP会员专享资源能否直接商用?
    本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
    提示下载完但解压或打开不了?
    最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,若小于网盘提示的容量则是这个原因。这是浏览器下载的bug,建议用百度网盘软件或迅雷下载。若排除这种情况,可在对应资源底部留言,或 联络我们.。
    找不到素材资源介绍文章里的示例图片?
    对于PPT,KEY,Mockups,APP,网页模版等类型的素材,文章内用于介绍的图片通常并不包含在对应可供下载素材包内。这些相关商业图片需另外购买,且本站不负责(也没有办法)找到出处。 同样地一些字体文件也是这种情况,但部分素材会在素材包内有一份字体下载链接清单。
    模板不会安装或需要功能定制以及二次开发?
    请QQ联系我们

    发表评论

    还没有评论,快来抢沙发吧!

    如需帝国cms功能定制以及二次开发请联系我们

    联系作者

    请选择支付方式

    ×
    迅虎支付宝
    迅虎微信
    支付宝当面付
    余额支付
    ×
    微信扫码支付 0 元