最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python Beautiful Soup模塊使用教程詳解

 更新時(shí)間:2023年02月24日 09:55:46   作者:Thunderclap_  
Beautiful Soup 簡稱 BS4(其中 4 表示版本號)是一個(gè) Python 中常用的頁面解析庫,它可以從 HTML 或 XML 文檔中快速地提取指定的數(shù)據(jù),這篇文章主要介紹了Python Beautiful Soup模塊的使用

一、模塊簡介

Beautiful Soup 是一個(gè)可以從HTML或XML文件中提取數(shù)據(jù)的Python庫.它能夠通過你喜歡的轉(zhuǎn)換器實(shí)現(xiàn)慣用的文檔導(dǎo)航,查找,修改文檔的方式.Beautiful Soup會幫你節(jié)省數(shù)小時(shí)甚至數(shù)天的工作時(shí)間.

二、方法利用

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  rel="external nofollow"  class="sister" id="link1">Elsie</a>,
<a  rel="external nofollow"  class="sister" id="link2">Lacie</a> and
<a  rel="external nofollow"  class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...</p>
"""
from bs4 import BeautifulSoup
soup = BeautifulSoup(html_doc, 'html.parser')

四種解析器

2、幾個(gè)簡單的瀏覽結(jié)構(gòu)化數(shù)據(jù)的方法

#獲取Tag,通俗點(diǎn)就是HTML中的一個(gè)個(gè)標(biāo)簽

#獲取Tag,通俗點(diǎn)就是HTML中的一個(gè)個(gè)標(biāo)簽
soup.title                    # 獲取整個(gè)title標(biāo)簽字段:<title>The Dormouse's story</title>
soup.title.name               # 獲取title標(biāo)簽名稱  :title
soup.title.parent.name        # 獲取 title 的父級標(biāo)簽名稱:head
soup.p                        # 獲取第一個(gè)p標(biāo)簽字段:<p class="title"><b>The Dormouse's story</b></p>
soup.p['class']               # 獲取第一個(gè)p中class屬性值:title
soup.p.get('class')           # 等價(jià)于上面
soup.a                        # 獲取第一個(gè)a標(biāo)簽字段
soup.find_all('a')            # 獲取所有a標(biāo)簽字段
soup.find(id="link3")         # 獲取屬性id值為link3的字段
soup.a['class'] = "newClass"  # 可以對這些屬性和內(nèi)容等等進(jìn)行修改
del bs.a['class']             # 還可以對這個(gè)屬性進(jìn)行刪除
soup.find('a').get('id')      # 獲取class值為story的a標(biāo)簽中id屬性的值
soup.title.string             # 獲取title標(biāo)簽的值  :The Dormouse's story

三、具體利用

1、獲取擁有指定屬性的標(biāo)簽

方法一:獲取單個(gè)屬性
soup.find_all('div',id="even")            # 獲取所有id=even屬性的div標(biāo)簽
soup.find_all('div',attrs={'id':"even"})    # 效果同上
方法二:
soup.find_all('div',id="even",class_="square")            # 獲取所有id=even并且class=square屬性的div標(biāo)簽
soup.find_all('div',attrs={"id":"even","class":"square"})    # 效果同上

2、獲取標(biāo)簽的屬性值

方法一:通過下標(biāo)方式提取
for link in soup.find_all('a'):
    print(link['href'])        //等同于 print(link.get('href'))
方法二:利用attrs參數(shù)提取
for link in soup.find_all('a'):
    print(link.attrs['href'])

3、獲取標(biāo)簽中的內(nèi)容

divs = soup.find_all('div')        # 獲取所有的div標(biāo)簽
for div in divs:                   # 循環(huán)遍歷div中的每一個(gè)div
    a = div.find_all('a')[0]      # 查找div標(biāo)簽中的第一個(gè)a標(biāo)簽      
    print(a.string)              # 輸出a標(biāo)簽中的內(nèi)容
如果結(jié)果沒有正確顯示,可以轉(zhuǎn)換為list列表

4、stripped_strings

去除\n換行符等其他內(nèi)容 stripped_strings

divs = soup.find_all('div')
for div in divs:
    infos = list(div.stripped_strings)        # 去掉空格換行等
    bring(infos)

四、輸出

1、格式化輸出prettify()

prettify() 方法將Beautiful Soup的文檔樹格式化后以Unicode編碼輸出,每個(gè)XML/HTML標(biāo)簽都獨(dú)占一行

markup = '<a  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >I linked to <i>example.com</i></a>'
soup = BeautifulSoup(markup)
soup.prettify()
# '<html>\n <head>\n </head>\n <body>\n  <a  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >\n...'
print(soup.prettify())
# <html>
#  <head>
#  </head>
#  <body>
#   <a  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >
#    I linked to
#    <i>
#     example.com
#    </i>
#   </a>
#  </body>
# </html>

2、get_text()

如果只想得到tag中包含的文本內(nèi)容,那么可以調(diào)用 get_text() 方法,這個(gè)方法獲取到tag中包含的所有文版內(nèi)容包括子孫tag中的內(nèi)容,并將結(jié)果作為Unicode字符串返回:

markup = '<a  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >\nI linked to <i>example.com</i>\n</a>'
soup = BeautifulSoup(markup)
soup.get_text()
u'\nI linked to example.com\n'
soup.i.get_text()
u'example.com'

到此這篇關(guān)于Python Beautiful Soup模塊使用教程詳解的文章就介紹到這了,更多相關(guān)Python Beautiful Soup內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Pytorch固定隨機(jī)數(shù)種子的方法小結(jié)

    Pytorch固定隨機(jī)數(shù)種子的方法小結(jié)

    在對神經(jīng)網(wǎng)絡(luò)模型進(jìn)行訓(xùn)練時(shí),有時(shí)候會存在對訓(xùn)練過程進(jìn)行復(fù)現(xiàn)的需求,然而,每次運(yùn)行時(shí) Pytorch、Numpy 中的隨機(jī)性將使得該目的變得困難重重,基于此,本文記錄了 Pytorch 中的固定隨機(jī)數(shù)種子的方法,需要的朋友可以參考下
    2023-12-12
  • 使用PyQt4 設(shè)置TextEdit背景的方法

    使用PyQt4 設(shè)置TextEdit背景的方法

    今天小編就為大家分享一篇使用PyQt4 設(shè)置TextEdit背景的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-06-06
  • Windows 下python3.8環(huán)境安裝教程圖文詳解

    Windows 下python3.8環(huán)境安裝教程圖文詳解

    這篇文章主要介紹了Windows 下python3.8環(huán)境安裝教程圖文詳解,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-03-03
  • 解決pycharm上的jupyter notebook端口被占用問題

    解決pycharm上的jupyter notebook端口被占用問題

    今天小編就為大家分享一篇解決pycharm上的jupyter notebook端口被占用問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • 使用Python將PDF表格提取到文本,CSV和Excel文件中

    使用Python將PDF表格提取到文本,CSV和Excel文件中

    本文將介紹如何使用簡單的Python代碼從PDF文檔中提取表格數(shù)據(jù)并將其寫入文本、CSV和Excel文件,從而輕松實(shí)現(xiàn)PDF表格的自動化提取,有需要的可以參考下
    2024-11-11
  • python中subplot大小的設(shè)置步驟

    python中subplot大小的設(shè)置步驟

    matploglib能夠繪制出精美的圖表,有時(shí)候我們希望把一組圖放在一起進(jìn)行比較,就需要用到matplotlib中提供的subplot了,這篇文章主要給大家介紹了關(guān)于python中subplot大小的設(shè)置方法,需要的朋友可以參考下
    2021-06-06
  • Python數(shù)據(jù)分析之?Pandas?Dataframe合并和去重操作

    Python數(shù)據(jù)分析之?Pandas?Dataframe合并和去重操作

    這篇文章主要介紹了Python數(shù)據(jù)分析之?Pandas?Dataframe合并和去重操作,文章基于python的相關(guān)資料展開詳細(xì)的內(nèi)容介紹,需要的小伙伴可以參考一下
    2022-05-05
  • Python本地與全局命名空間用法實(shí)例

    Python本地與全局命名空間用法實(shí)例

    這篇文章主要介紹了Python本地與全局命名空間用法,實(shí)例分析了Python命名空間的相關(guān)使用技巧,需要的朋友可以參考下
    2015-06-06
  • python list多級排序知識點(diǎn)總結(jié)

    python list多級排序知識點(diǎn)總結(jié)

    在本篇文章里小編給大家分享的是關(guān)于python list多級排序的相關(guān)知識點(diǎn)內(nèi)容,有需要的朋友們學(xué)習(xí)下。
    2019-10-10
  • Python學(xué)習(xí)之不同數(shù)據(jù)類型間的轉(zhuǎn)換總結(jié)

    Python學(xué)習(xí)之不同數(shù)據(jù)類型間的轉(zhuǎn)換總結(jié)

    類型轉(zhuǎn)換,就是將自身的數(shù)據(jù)類型變成新的數(shù)據(jù)類型,并擁有新的數(shù)據(jù)類型的所有功能的過程。本文將詳細(xì)為大家介紹如何在Python中實(shí)現(xiàn)不同數(shù)據(jù)類型的轉(zhuǎn)換,感興趣的可以了解一下
    2022-03-03

最新評論

沙雅县| 通化县| 卓资县| 广元市| 永胜县| 广安市| 湘阴县| 朝阳区| 枣强县| 德江县| 雷州市| 海宁市| 横山县| 防城港市| 苍山县| 黑山县| 工布江达县| 芒康县| 麻江县| 宁晋县| 新化县| 页游| 慈利县| 定州市| 云霄县| 乌苏市| 始兴县| 伊吾县| 大洼县| 象山县| 新邵县| 梓潼县| 岑巩县| 哈密市| 囊谦县| 昌吉市| 台东县| 凤阳县| 临邑县| 揭阳市| 聊城市|