日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python解析库beautifulsoup_12_Python_解析库_BeautifulSoup的使用

發布時間:2025/5/22 python 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python解析库beautifulsoup_12_Python_解析库_BeautifulSoup的使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、安裝

pip3 install BeautifulSoup

Beautiful Soup支持的解析器

解析器

使用方法

優勢

劣勢

Python標準庫

BeautifulSoup(markup, "html.parser")

Python的內置標準庫、執行速度適中、文檔容錯能力強

Python 2.7.3及Python 3.2.2之前的版本文檔容錯能力差

lxml HTML解析器

BeautifulSoup(markup, "lxml")

速度快、文檔容錯能力強

需要安裝C語言庫

lxml XML解析器

BeautifulSoup(markup, "xml")

速度快、唯一支持XML的解析器

需要安裝C語言庫

html5lib

BeautifulSoup(markup, "html5lib")

最好的容錯性、以瀏覽器的方式解析文檔、生成HTML5格式的文檔

速度慢、不依賴外部擴展

2、基本使用方法

2.1、查找元素

html = """

我是html文件

我是 p 標簽

"""

#導入方法

from bs4 import BeautifulSoup

#初始化 html 文件

soup = BeautifulSoup(html, 'lxml')

# 1 獲取 title 標簽

title = soup.title

print(title)

#輸出結果:

我是html文件

# 2 獲取 p 標簽

p = soup.p

print(p)

#輸出結果:

我是 p 標簽

2.2、獲取屬性

html = '''

我是 p 標簽

'''

from bs4 import BeautifulSoup

soup = BeautifulSoup(html, 'lxml')

# 1 直接傳入中括號和屬性名,獲取 p 標簽 name 的屬性

name_1 = soup.p['name']

# 輸出結果:hello

# 2 使用attrs屬性獲取 p 標簽 name 的屬性

name_2 = soup.p.attrs['name']

# 輸出結果:hello

2.3、獲取內容

html = '''

我是 p 標簽

'''

from bs4 import BeautifulSoup

soup = BeautifulSoup(html, 'lxml')

# 1 使用string屬性獲取 p 標簽的文本信息

text_1 = soup.p.string

print(text_1)

# 輸出結果:我是 p 標簽

# 2 使用 get_text()方法獲取 p 標簽的文本信息

text_2 = soup.p.get_text()

print(text_2)

# 輸出結果:我是 p 標簽

2.4、嵌套選擇

html = '''

我是 p 標簽

'''

from bs4 import BeautifulSoup

soup = BeautifulSoup(html, 'lxml')

# 1 使用string屬性獲取 title 標簽里面 p 標簽的文本內容

text_1 = soup.title.p.string

print(text_1)

# 輸出結果:我是 p 標簽

# 2 使用 get_text()方法獲取 title 標簽里面 p 標簽的文本內容

text_2 = soup.title.p.get_text()

print(text_2)

# 輸出結果:我是 p 標簽

3、關聯選擇

3.1、子節點

contents:獲取所有直接子節點,返回結果會是列表形式。

children:獲取所有子節點,返回結果是生成器類型,可以用for循環輸出相應的內容。

html = """

  • 水果菜單

    小香蕉

"""

from bs4 import BeautifulSoup

soup = BeautifulSoup(html, 'lxml')

'''contents:獲取所有直接子節點,返回結果會是列表形式。'''

result_01 = soup.ul.contents

print(result_01)

# 輸出結果:['\n',

水果菜單]

'''children:獲取所有子節點,返回結果是生成器類型,可以用for循環輸出相應的內容。'''

result_02 = soup.ul.children

for i in result_02:

print(i)

# 輸出結果:

水果菜單

3.2、子孫節點

descendants屬性獲取所有子孫節點,返回結果也是生成器,descendants會遞歸查詢所有子節點,得到所有的子孫節點。

html = """

  • 水果菜單

    小香蕉

"""

from bs4 import BeautifulSoup

soup = BeautifulSoup(html, 'lxml')

'''descendants屬性獲取所有子孫節點,返回結果也是生成器,descendants會遞歸查詢所有子節點,得到所有的子孫節點。 '''

result = soup.ul.descendants

for i in result:

print(i)

# 輸出結果:

"""

水果菜單

小香蕉

水果菜單

小香蕉

香蕉

小香蕉

小香蕉

"""

3.3、父節點、祖先節點

parent屬性獲取父節點

parents屬性可以遞歸得到元素的所有父輩節點

html = """

  • 水果菜單

    小香蕉

"""

from bs4 import BeautifulSoup

soup = BeautifulSoup(html, 'lxml')

'''parent屬性獲取父節點'''

print(soup.a.parent)

# 輸出結果:

'''

小香蕉

'''

'''parents屬性可以遞歸得到元素的所有父輩節點'''

result = soup.a.parents

for i in result:

print(i)

3.4 兄弟節點

next_sibling :查找兄弟節點的下一個標簽 。

previous_sibling:查找兄弟節點的前一個標簽 。

next_siblings:對當前節點的兄弟節點迭代輸出 。

previous_siblings :對當前節點的兄弟節點迭代輸出。

html = """

  • 水果菜單

    蘋果

    小香蕉

"""

from bs4 import BeautifulSoup

soup = BeautifulSoup(html, 'lxml')

'''next_sibling 屬性獲取節點下一個兄弟元素'''

result_1 = soup.p.next_sibling

print(result_1)

'''next_siblings 屬性獲取所有前面的節點元素'''

result_2 = soup.p.next_siblings

for i in result_2:

print(i)

'''previous_sibling 屬性獲取上一個兄弟元素'''

result_3 = soup.p.previous_sibling

print(result_3)

'''previous_siblings 屬性獲取后面的所有節點元素'''

result_4 = soup.p.previous_siblings

for i in result_2:

print(i)

4、find_all()方法選擇器

4、find_all():顧名思義,就是查詢所有符合條件的元素。

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

4.1、name

根據節點名來查詢元素,示例如下:

html = """

蘋果

香蕉皇帝蕉

可樂

"""

from bs4 import BeautifulSoup

soup = BeautifulSoup(html, 'lxml')

'''根據name值查找元素'''

a = soup.find_all(name="a")

print(a)

# 輸出結果:[蘋果, 香蕉皇帝蕉]

'''根據name值進行嵌套查詢'''

for a in soup.find_all(name="a"):

span = a.find_all(name='span')

print(span)

# 輸出結果: [皇帝蕉]

4.2、attrs={'key':'value'}

根據標簽的屬性來進行查詢,示例如下:

html = """

蘋果

咖啡

可樂

"""

from bs4 import BeautifulSoup

soup = BeautifulSoup(html, 'lxml')

'''根據 id 的值來查找 '''

id_1 = soup.find_all(attrs={'id': 'item_1'})

id_2 = soup.find_all(id="item_1") # 也可以直接傳入id這個參數進行查找。

'''根據 class 的值來查找 '''

class_1 = soup.find_all(attrs={'class': 'coffee'})

class_2 = soup.find_all(class_="coffee") # 由于class在Python里是一個關鍵字,可以直接在class后面加一個下劃線(class_)進行查找。

【注意】

1 傳入的attrs參數,參數的類型是字典類型。

2 查詢后得到的結果是列表形式。

3 直接傳入 class 這個參數來查找,后面需要加一個下劃線。

4.3、text

text參數可用來匹配節點的文本,傳入的形式可以是字符串,可以是正則表達式對象,示例如下:

html = """

蘋果apple

咖啡coffee

可樂

"""

import re

from bs4 import BeautifulSoup

soup = BeautifulSoup(html, 'lxml')

'''查找文本信息中含有apple的元素'''

apple = re.compile('apple')

result = soup.find_all(text=apple)

print(result)

# 輸出結果:['蘋果apple']

'''查找text="可樂"的元素'''

result = soup.find_all(text="可樂")

print(result)

# 輸出結果:['可樂']

5、find()方法選擇器

find()方法:find()和find_all()的使用是差不多的,唯一的區別是find()返回的是單個元素,也就是第一個匹配的元素。

html = """

蘋果apple

可樂

"""

from bs4 import BeautifulSoup

soup = BeautifulSoup(html, 'lxml')

'''1、find()方法:返回的是單個元素,也就是第一個匹配的元素。'''

item = soup.find(class_='item')

print(item)

# 輸出結果:

蘋果apple

'''2、find_all()方法:返回的是所有匹配的元素組成的列表。'''

items = soup.find_all(class_='item')

print(items)

# 輸出結果:[

蘋果apple

,

可樂

]

6、其余的方法選擇器

- find_parents()和find_parent():前者返回所有祖先節點,后者返回直接父節點。

- find_next_siblings()和find_next_sibling():前者返回后面所有的兄弟節點,后者返回后面第一個兄弟節點。

- find_previous_siblings()和find_previous_sibling():前者返回前面所有的兄弟節點,后者返回前面第一個兄弟節點。

- find_all_next()和find_next():前者返回節點后所有符合條件的節點,后者返回第一個符合條件的節點。

- find_all_previous()和find_previous():前者返回節點后所有符合條件的節點,后者返回第一個符合條件的節點。

7、 CSS選擇器

使用CSS選擇器查找元素時,只需要調用select()方法,傳入相應的CSS選擇器即可,示例如下:

7.1、select()方法基本使用

html = '''

飲料套餐

咖啡

牛奶

'''

from bs4 import BeautifulSoup

soup = BeautifulSoup(html, 'lxml')

'''1、查找 class=drink 元素里面 id=coffee 的元素'''

print(soup.select('.drink #coffee'))

'''2、查找 id =coffee 的元素'''

print(soup.select('#coffee'))

'''3、查找p 標簽下面的 a 標簽的所有元素'''

print(soup.select('p a'))

7.2、獲取元素屬性

html = '''

飲料套餐

咖啡

牛奶

'''

from bs4 import BeautifulSoup

soup = BeautifulSoup(html, 'lxml')

'''獲取 p 標簽元素中 a 標簽的所有元素'''

a = soup.select('a')

'''獲取a 標簽元素的id屬性'''

for i in a:

result = i['id']

result = i.attrs['id'] # 使用attrs屬性獲取屬性名

print(result)

# 輸出結果:

'''

coffee

milk

'''

7.3、獲取元素文本信息

獲取文本信息的方法有:

string屬性

get_text()的方法

text

html = '''

飲料套餐

咖啡

牛奶

'''

from bs4 import BeautifulSoup

soup = BeautifulSoup(html, 'lxml')

'''獲取id=coffee的元素'''

coffee = soup.select('#coffee')

for i in coffee:

result_1 = i.text

result_2 = i.string

result_3 = i.get_text()

print(result_1,result_2,result_3)

# 輸出結果:咖啡 咖啡 咖啡

參考資料:

總結

以上是生活随笔為你收集整理的python解析库beautifulsoup_12_Python_解析库_BeautifulSoup的使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。