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

使用Python爬蟲庫BeautifulSoup遍歷文檔樹并對標簽進行操作詳解

 更新時間:2020年01月25日 16:19:08   作者:BQW_  
今天為大家介紹下Python爬蟲庫BeautifulSoup遍歷文檔樹并對標簽進行操作的詳細方法與函數(shù)

下面就是使用Python爬蟲庫BeautifulSoup對文檔樹進行遍歷并對標簽進行操作的實例,都是最基礎(chǔ)的內(nèi)容

html_doc = """
<html><head><title>The Dormouse's story</title></head>

<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" 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" 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" 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,'lxml')

一、子節(jié)點

一個Tag可能包含多個字符串或者其他Tag,這些都是這個Tag的子節(jié)點.BeautifulSoup提供了許多操作和遍歷子結(jié)點的屬性。

1.通過Tag的名字來獲得Tag

print(soup.head)
print(soup.title)
<head><title>The Dormouse's story</title></head>
<title>The Dormouse's story</title>

通過名字的方法只能獲得第一個Tag,如果要獲得所有的某種Tag可以使用find_all方法

soup.find_all('a')
[<a class="sister"  rel="external nofollow" 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" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link2">Lacie</a>,
 <a class="sister"  rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link3">Tillie</a>]

2.contents屬性:將Tag的子節(jié)點通過列表的方式返回

head_tag = soup.head
head_tag.contents
[<title>The Dormouse's story</title>]
title_tag = head_tag.contents[0]
title_tag
<title>The Dormouse's story</title>
title_tag.contents
["The Dormouse's story"]

3.children:通過該屬性對子節(jié)點進行循環(huán)

for child in title_tag.children:
  print(child)
The Dormouse's story

4.descendants: 不論是contents還是children都是返回直接子節(jié)點,而descendants對所有tag的子孫節(jié)點進行遞歸循環(huán)

for child in head_tag.children:
  print(child)
<title>The Dormouse's story</title>
for child in head_tag.descendants:
  print(child)
<title>The Dormouse's story</title>
The Dormouse's story

5.string 如果tag只有一個NavigableString類型的子節(jié)點,那么tag可以使用.string得到該子節(jié)點

title_tag.string
"The Dormouse's story"

如果一個tag只有一個子節(jié)點,那么使用.string可以獲得其唯一子結(jié)點的NavigableString.

head_tag.string
"The Dormouse's story"

如果tag有多個子節(jié)點,tag無法確定.string對應(yīng)的是那個子結(jié)點的內(nèi)容,故返回None

print(soup.html.string)
None

6.strings和stripped_strings

如果tag包含多個字符串,可以使用.strings循環(huán)獲取

for string in soup.strings:
  print(string)
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.


...

.string輸出的內(nèi)容包含了許多空格和空行,使用strpped_strings去除這些空白內(nèi)容

for string in soup.stripped_strings:
  print(string)
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.
...

二、父節(jié)點

1.parent:獲得某個元素的父節(jié)點

title_tag = soup.title
title_tag.parent
<head><title>The Dormouse's story</title></head>

字符串也有父節(jié)點

title_tag.string.parent
<title>The Dormouse's story</title>

2.parents:遞歸的獲得所有父輩節(jié)點

link = soup.a
for parent in link.parents:
  if parent is None:
    print(parent)
  else:
    print(parent.name)
p
body
html
[document]

三、兄弟結(jié)點

sibling_soup = BeautifulSoup("<a><b>text1</b><c>text2</c></b></a>",'lxml')
print(sibling_soup.prettify())
<html>
 <body>
 <a>
  <b>
  text1
  </b>
  <c>
  text2
  </c>
 </a>
 </body>
</html>

1.next_sibling和previous_sibling

sibling_soup.b.next_sibling
<c>text2</c>
sibling_soup.c.previous_sibling
<b>text1</b>

在實際文檔中.next_sibling和previous_sibling通常是字符串或者空白符

soup.find_all('a')
[<a class="sister"  rel="external nofollow" 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" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link2">Lacie</a>,
 <a class="sister"  rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link3">Tillie</a>]
soup.a.next_sibling # 第一個<a></a>的next_sibling是,\n
',\n'
soup.a.next_sibling.next_sibling
<a class="sister"  rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link2">Lacie</a>

2.next_siblings和previous_siblings

for sibling in soup.a.next_siblings:
  print(repr(sibling))
',\n'
<a class="sister"  rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link2">Lacie</a>
' and\n'
<a class="sister"  rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link3">Tillie</a>
';\nand they lived at the bottom of a well.'
for sibling in soup.find(id="link3").previous_siblings:
  print(repr(sibling))
' and\n'
<a class="sister"  rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link2">Lacie</a>
',\n'
<a class="sister"  rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link1">Elsie</a>
'Once upon a time there were three little sisters; and their names were\n'

四、回退與前進

1.next_element和previous_element

指向下一個或者前一個被解析的對象(字符串或tag),即深度優(yōu)先遍歷的后序節(jié)點和前序節(jié)點

last_a_tag = soup.find("a", id="link3")
print(last_a_tag.next_sibling)
print(last_a_tag.next_element)
;
and they lived at the bottom of a well.
Tillie
last_a_tag.previous_element
' and\n'

2.next_elements和previous_elements

通過.next_elements和previous_elements可以向前或向后訪問文檔的解析內(nèi)容,就好像文檔正在被解析一樣

for element in last_a_tag.next_elements:
  print(repr(element))
'Tillie'
';\nand they lived at the bottom of a well.'
'\n'
<p class="story">...</p>
'...'
'\n'

更多關(guān)于使用Python爬蟲庫BeautifulSoup遍歷文檔樹并對標簽進行操作的方法與文章大家可以點擊下面的相關(guān)文章

相關(guān)文章

  • 整理Python中的賦值運算符

    整理Python中的賦值運算符

    這篇文章主要介紹了Python中的賦值運算符,其使用是Python的基本功,需要的朋友可以參考下
    2015-05-05
  • Flask之閃現(xiàn)flash原理及使用

    Flask之閃現(xiàn)flash原理及使用

    Flask中的閃現(xiàn)是一種在請求之間傳遞消息的機制,本文就來介紹一下Flask之閃現(xiàn)flash原理及使用,具有一定的參考價值,感興趣的可以了解一下
    2023-12-12
  • 基于Python編寫簡易文字語音轉(zhuǎn)換器

    基于Python編寫簡易文字語音轉(zhuǎn)換器

    這篇文章主要為大家介紹了如何利用Python編寫一個簡易文字語音轉(zhuǎn)換器,并打包成exe。文中的示例代碼講解詳細,感興趣的小伙伴快跟隨小編一起嘗試一下
    2022-03-03
  • Linux環(huán)境下GPU版本的pytorch安裝

    Linux環(huán)境下GPU版本的pytorch安裝

    使用默認的源地址下載速度很慢,所以一般都是使用國內(nèi)源,今天花了點時間配置安裝,所以記錄一下,需要的朋友們下面隨著小編來一起學習學習吧
    2021-05-05
  • Python讀取預處理DICOM文件方式詳解

    Python讀取預處理DICOM文件方式詳解

    這篇文章主要介紹了Python讀取預處理DICOM文件方式,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-03-03
  • python實現(xiàn)貪吃蛇雙人大戰(zhàn)

    python實現(xiàn)貪吃蛇雙人大戰(zhàn)

    這篇文章主要為大家詳細介紹了python實現(xiàn)貪吃蛇雙人大戰(zhàn),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-04-04
  • 六個Python編程最受用的內(nèi)置函數(shù)使用詳解

    六個Python編程最受用的內(nèi)置函數(shù)使用詳解

    在日常的python編程中使用這幾個函數(shù)來簡化我們的編程工作,經(jīng)常使用能使編程效率大大地提高。本文為大家總結(jié)了六個Python編程最受用的內(nèi)置函數(shù),感興趣的可以了解一下
    2022-07-07
  • python進程管理工具supervisor使用實例

    python進程管理工具supervisor使用實例

    這篇文章主要介紹了python進程管理工具supervisor使用實例,本文介紹了supervisor的安裝、配置、使用等內(nèi)容,需要的朋友可以參考下
    2014-09-09
  • python 將字符串中的數(shù)字相加求和的實現(xiàn)

    python 將字符串中的數(shù)字相加求和的實現(xiàn)

    這篇文章主要介紹了python 將字符串中的數(shù)字相加求和的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-07-07
  • Django實現(xiàn)將views.py中的數(shù)據(jù)傳遞到前端html頁面,并展示

    Django實現(xiàn)將views.py中的數(shù)據(jù)傳遞到前端html頁面,并展示

    這篇文章主要介紹了Django實現(xiàn)將views.py中的數(shù)據(jù)傳遞到前端html頁面并展示,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03

最新評論

宁化县| 牟定县| 房产| 揭东县| 岐山县| 汝阳县| 绥宁县| 怀柔区| 泾阳县| 伊金霍洛旗| 贺兰县| 深水埗区| 承德县| 利辛县| 台中县| 获嘉县| 汝阳县| 噶尔县| 茌平县| 凤凰县| 柘城县| 甘洛县| 盐边县| 石河子市| 呼伦贝尔市| 弥渡县| 楚雄市| 许昌县| 象州县| 西畴县| 巴林左旗| 东兰县| 阿拉尔市| 吕梁市| 深泽县| 高邮市| 通化市| 阿拉尔市| 建始县| 浦江县| 泾源县|