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

面向新手解析python Beautiful Soup基本用法

 更新時(shí)間:2020年07月11日 08:37:28   作者:夏日的向日葵  
這篇文章主要介紹了面向新手解析python Beautiful Soup基本用法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

Beautiful Soup就是Python的一個(gè)HTML或XML的解析庫(kù),可以用它來(lái)方便地從網(wǎng)頁(yè)中提取數(shù)據(jù)。它有如下三個(gè)特點(diǎn):

  • Beautiful Soup提供一些簡(jiǎn)單的、Python式的函數(shù)來(lái)處理導(dǎo)航、搜索、修改分析樹等功能。它是一個(gè)工具箱,通過(guò)解析文檔為用戶提供需要抓取的數(shù)據(jù),因?yàn)楹?jiǎn)單,所以不需要多少代碼就可以寫出一個(gè)完整的應(yīng)用程序。
  • Beautiful Soup自動(dòng)將輸入文檔轉(zhuǎn)換為Unicode編碼,輸出文檔轉(zhuǎn)換為UTF-8編碼。你不需要考慮編碼方式,除非文檔沒(méi)有指定一個(gè)編碼方式,這時(shí)你僅僅需要說(shuō)明一下原始編碼方式就可以了。
  • Beautiful Soup已成為和lxml、html6lib一樣出色的Python解釋器,為用戶靈活地提供不同的解析策略或強(qiáng)勁的速度。

首先,我們要安裝它:pip install bs4,然后安裝 pip install beautifulsoup4.

Beautiful Soup支持的解析器

下面我們以lxml解析器為例:

from bs4 import BeautifulSoup
soup = BeautifulSoup('<p>Hello</p>', 'lxml')
print(soup.p.string)

結(jié)果:

Hello

beautiful soup美化的效果實(shí)例:

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  rel="external nofollow" rel="external nofollow" rel="external nofollow" class="sister" id="link1"><!-- Elsie --></a>,
<a  rel="external nofollow" rel="external nofollow" rel="external nofollow" class="sister" id="link2">Lacie</a> and
<a  rel="external nofollow" rel="external nofollow" 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, 'lxml')#調(diào)用prettify()方法。這個(gè)方法可以把要解析的字符串以標(biāo)準(zhǔn)的縮進(jìn)格式輸出
print(soup.prettify())
print(soup.title.string)

結(jié)果:

<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 class="sister"  rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link1">
  <!-- Elsie -->
  </a>
  ,
  <a class="sister"  rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link2">
  Lacie
  </a>
  and
  <a class="sister"  rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link3">
  Tillie
  </a>
  ;
and they lived at the bottom of a well.
 </p>
 <p class="story">
  ...
 </p>
 </body>
</html>
The Dormouse's story

下面舉例說(shuō)明選擇元素、屬性、名稱的方法

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  rel="external nofollow" rel="external nofollow" rel="external nofollow" class="sister" id="link1"><!-- Elsie --></a>,
<a  rel="external nofollow" rel="external nofollow" rel="external nofollow" class="sister" id="link2">Lacie</a> and
<a  rel="external nofollow" rel="external nofollow" 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, 'lxml')
print('輸出結(jié)果為title節(jié)點(diǎn)加里面的文字內(nèi)容:\n',soup.title)
print('輸出它的類型:\n',type(soup.title))
print('輸出節(jié)點(diǎn)的文本內(nèi)容:\n',soup.title.string)
print('結(jié)果是節(jié)點(diǎn)加其內(nèi)部的所有內(nèi)容:\n',soup.head)
print('結(jié)果是第一個(gè)p節(jié)點(diǎn)的內(nèi)容:\n',soup.p)
print('利用name屬性獲取節(jié)點(diǎn)的名稱:\n',soup.title.name)
#這里需要注意的是,有的返回結(jié)果是字符串,有的返回結(jié)果是字符串組成的列表。
# 比如,name屬性的值是唯一的,返回的結(jié)果就是單個(gè)字符串。
# 而對(duì)于class,一個(gè)節(jié)點(diǎn)元素可能有多個(gè)class,所以返回的是列表。
print('每個(gè)節(jié)點(diǎn)可能有多個(gè)屬性,比如id和class等:\n',soup.p.attrs)
print('選擇這個(gè)節(jié)點(diǎn)元素后,可以調(diào)用attrs獲取所有屬性:\n',soup.p.attrs['name'])
print('獲取p標(biāo)簽的name屬性值:\n',soup.p['name'])
print('獲取p標(biāo)簽的class屬性值:\n',soup.p['class'])
print('獲取第一個(gè)p節(jié)點(diǎn)的文本:\n',soup.p.string)

結(jié)果:

輸出結(jié)果為title節(jié)點(diǎn)加里面的文字內(nèi)容:
<title>The Dormouse's story</title>
輸出它的類型:
<class 'bs4.element.Tag'>
輸出節(jié)點(diǎn)的文本內(nèi)容:
The Dormouse's story
結(jié)果是節(jié)點(diǎn)加其內(nèi)部的所有內(nèi)容:
<head><title>The Dormouse's story</title></head>
結(jié)果是第一個(gè)p節(jié)點(diǎn)的內(nèi)容:
<p class="title" name="dromouse"><b>The Dormouse's story</b></p>
利用name屬性獲取節(jié)點(diǎn)的名稱:
title
每個(gè)節(jié)點(diǎn)可能有多個(gè)屬性,比如id和class等:
{'class': ['title'], 'name': 'dromouse'}
選擇這個(gè)節(jié)點(diǎn)元素后,可以調(diào)用attrs獲取所有屬性:
dromouse
獲取p標(biāo)簽的name屬性值:
dromouse
獲取p標(biāo)簽的class屬性值:
['title']
獲取第一個(gè)p節(jié)點(diǎn)的文本:
The Dormouse's story

在上面的例子中,我們知道每一個(gè)返回結(jié)果都是bs4.element.Tag類型,它同樣可以繼續(xù)調(diào)用節(jié)點(diǎn)進(jìn)行下一步的選擇。

html = """
<html><head><title>The Dormouse's story</title></head>
<body>
"""
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, 'lxml')
print('獲取了head節(jié)點(diǎn)元素,繼續(xù)調(diào)用head來(lái)選取其內(nèi)部的head節(jié)點(diǎn)元素:\n',soup.head.title)
print('繼續(xù)調(diào)用輸出類型:\n',type(soup.head.title))
print('繼續(xù)調(diào)用輸出內(nèi)容:\n',soup.head.title.string)

結(jié)果:

獲取了head節(jié)點(diǎn)元素,繼續(xù)調(diào)用head來(lái)選取其內(nèi)部的head節(jié)點(diǎn)元素:
 <title>The Dormouse's story</title>
繼續(xù)調(diào)用輸出類型:
 <class 'bs4.element.Tag'>
繼續(xù)調(diào)用輸出內(nèi)容:
 The Dormouse's story

(1)find_all()

find_all,顧名思義,就是查詢所有符合條件的元素。給它傳入一些屬性或文本,就可以得到符合條件的元素,它的功能十分強(qiáng)大。

find_all(name , attrs , recursive , text , **kwargs)

他的用法:

html='''
<div class="panel">
  <div class="panel-heading">
    <h4>Hello</h4>
  </div>
  <div class="panel-body">
    <ul class="list" id="list-1">
      <li class="element">Foo</li>
      <li class="element">Bar</li>
      <li class="element">Jay</li>
    </ul>
    <ul class="list list-small" id="list-2">
      <li class="element">Foo</li>
      <li class="element">Bar</li>
    </ul>
  </div>
</div>
'''
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, 'lxml')
print('查詢所有ul節(jié)點(diǎn),返回結(jié)果是列表類型,長(zhǎng)度為2:\n',soup.find_all(name='ul'))
print('每個(gè)元素依然都是bs4.element.Tag類型:\n',type(soup.find_all(name='ul')[0]))
#將以上步驟換一種方式,遍歷出來(lái)
for ul in soup.find_all(name='ul'):
  print('輸出每個(gè)u1:',ul.find_all(name='li'))
#遍歷兩層
for ul in soup.find_all(name='ul'):
  print('輸出每個(gè)u1:',ul.find_all(name='li'))
  for li in ul.find_all(name='li'):
    print('輸出每個(gè)元素:',li.string)

結(jié)果:

查詢所有ul節(jié)點(diǎn),返回結(jié)果是列表類型,長(zhǎng)度為2:
 [<ul class="list" id="list-1">
<li class="element">Foo</li>
<li class="element">Bar</li>
<li class="element">Jay</li>
</ul>, <ul class="list list-small" id="list-2">
<li class="element">Foo</li>
<li class="element">Bar</li>
</ul>]
每個(gè)元素依然都是bs4.element.Tag類型:
 <class 'bs4.element.Tag'>
輸出每個(gè)u1: [<li class="element">Foo</li>, <li class="element">Bar</li>, <li class="element">Jay</li>]
輸出每個(gè)u1: [<li class="element">Foo</li>, <li class="element">Bar</li>]
輸出每個(gè)u1: [<li class="element">Foo</li>, <li class="element">Bar</li>, <li class="element">Jay</li>]
輸出每個(gè)元素: Foo
輸出每個(gè)元素: Bar
輸出每個(gè)元素: Jay
輸出每個(gè)u1: [<li class="element">Foo</li>, <li class="element">Bar</li>]
輸出每個(gè)元素: Foo
輸出每個(gè)元素: Bar

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python matplotlib可視化實(shí)例解析

    Python matplotlib可視化實(shí)例解析

    這篇文章主要介紹了Python matplotlib可視化實(shí)例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06
  • python如何利用中心坐標(biāo)繪制矩形

    python如何利用中心坐標(biāo)繪制矩形

    這篇文章主要介紹了python如何利用中心坐標(biāo)繪制矩形問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • Python實(shí)現(xiàn)在PDF中添加數(shù)字簽名

    Python實(shí)現(xiàn)在PDF中添加數(shù)字簽名

    無(wú)論是商業(yè)文件、法律文件還是個(gè)人文件,都可能需要證明其來(lái)源的真實(shí)性和完整性,PDF數(shù)字簽名就是解決這些問(wèn)題的關(guān)鍵工具,下面我們來(lái)看看如何使用?Python?為PDF文檔添加數(shù)字簽名吧
    2025-01-01
  • 在python win系統(tǒng)下 打開TXT文件的實(shí)例

    在python win系統(tǒng)下 打開TXT文件的實(shí)例

    下面小編就為大家分享一篇在python win系統(tǒng)下 打開TXT文件的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-04-04
  • Python math 模塊完全指南

    Python math 模塊完全指南

    本文主要介紹了Python math 模塊完全指南,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06
  • python 辦公自動(dòng)化——基于pyqt5和openpyxl統(tǒng)計(jì)符合要求的名單

    python 辦公自動(dòng)化——基于pyqt5和openpyxl統(tǒng)計(jì)符合要求的名單

    前幾天接到的一個(gè)需求,因?yàn)閷W(xué)校給的名單是青年大學(xué)習(xí)已學(xué)習(xí)的名單,然而要知道未學(xué)習(xí)的名單只能從所有團(tuán)員中再排查一次,過(guò)程相當(dāng)麻煩。剛好我也學(xué)過(guò)一些操作辦公軟件的基礎(chǔ),再加上最近在學(xué)pyqt5,所以我決定用python寫個(gè)自動(dòng)操作文件的腳本給她用用。
    2021-05-05
  • Python基礎(chǔ)之time庫(kù)詳解

    Python基礎(chǔ)之time庫(kù)詳解

    這篇文章主要介紹了Python基礎(chǔ)之time庫(kù)詳解,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)python基礎(chǔ)的小伙伴們有非常好的幫助,需要的朋友可以參考下
    2021-04-04
  • 利用PyInstaller將python程序.py轉(zhuǎn)為.exe的方法詳解

    利用PyInstaller將python程序.py轉(zhuǎn)為.exe的方法詳解

    這篇文章主要給大家介紹了利用PyInstaller將python程序.py轉(zhuǎn)為.exe的方法,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。
    2017-05-05
  • Python+Turtle繪制蜘蛛俠的示例代碼

    Python+Turtle繪制蜘蛛俠的示例代碼

    蜘蛛俠(Spider-Man)即彼得·帕克(Peter Parker),是美國(guó)漫威漫畫旗下超級(jí)英雄。本文主要介紹運(yùn)用python中的turtle庫(kù)控制函數(shù)繪制蜘蛛俠,感興趣的可以嘗試一下
    2022-06-06
  • Python解決MySQL數(shù)據(jù)處理從SQL批量刪除報(bào)錯(cuò)

    Python解決MySQL數(shù)據(jù)處理從SQL批量刪除報(bào)錯(cuò)

    這篇文章主要為大家介紹了Python解決MySQL數(shù)據(jù)處理從SQL批量刪除報(bào)錯(cuò),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-12-12

最新評(píng)論

东海县| 沙湾县| 青田县| 抚松县| 宣城市| 乐业县| 页游| 西吉县| 虎林市| 仲巴县| 扎鲁特旗| 凤庆县| 银川市| 德阳市| 密云县| 新津县| 田东县| 县级市| 凌云县| 株洲市| 武功县| 蒲江县| 陵水| 苍南县| 浏阳市| 九江县| 连平县| 三亚市| 嵩明县| 柘荣县| 恩施市| 唐河县| 阳信县| 墨江| 信丰县| 元氏县| 甘泉县| 肇源县| 潼南县| 偏关县| 石狮市|