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

歡迎訪問 生活随笔!

生活随笔

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

python

python 中BeautifulSoup入门

發布時間:2025/3/15 python 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 中BeautifulSoup入门 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

什么是BeautifulSoup?

Beautiful Soup?是用Python寫的一個HTML/XML的解析器,它可以很好的處理不規范標記并生成剖析樹(parse tree)。 它提供簡單又常用的導航(navigating),搜索以及修改剖析樹的操作。它可以大大節省你的編程時間。

直接看例子:

#!/usr/bin/python
# -*- coding: utf-8 -*-

from bs4 import BeautifulSoup

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 href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>

<p class="story">...</p>

"""

soup = BeautifulSoup(html_doc)

print soup.title

print soup.title.name

print soup.title.string

print soup.p

print soup.a

print soup.find_all('a')

print soup.find(id='link3')

print soup.get_text()

結果為:

<title>The Dormouse's story</title>
title
The Dormouse's story
<p class="title"><b>The Dormouse's story</b></p>
<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>
[<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]
<a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>

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.
...

可以看出:soup 就是BeautifulSoup處理格式化后的字符串,soup.title 得到的是title標簽,soup.p ?得到的是文檔中的第一個p標簽,要想得到所有標簽,得用find_all

函數。find_all 函數返回的是一個序列,可以對它進行循環,依次得到想到的東西.

get_text() 是返回文本,這個對每一個BeautifulSoup處理后的對象得到的標簽都是生效的。你可以試試?print soup.p.get_text()

其實是可以獲得標簽的其他屬性的,比如我要獲得a標簽的href屬性的值,可以使用 print soup.a['href'],類似的其他屬性,比如class也是可以這么得到的(soup.a['class'])。

特別的,一些特殊的標簽,比如head標簽,是可以通過soup.head 得到,其實前面也已經說了。

如何獲得標簽的內容數組?使用contents 屬性就可以 比如使用?print soup.head.contents,就獲得了head下的所有子孩子,以列表的形式返回結果,

可以使用 [num] ?的形式獲得 ,獲得標簽,使用.name 就可以。

獲取標簽的孩子,也可以使用children,但是不能print soup.head.children 沒有返回列表,返回的是?<listiterator object at 0x108e6d150>,

不過使用list可以將其轉化為列表。當然可以使用for 語句遍歷里面的孩子。

關于string屬性,如果超過一個標簽的話,那么就會返回None,否則就返回具體的字符串print soup.title.string 就返回了?The Dormouse's story

超過一個標簽的話,可以試用strings

向上查找可以用parent函數,如果查找所有的,那么可以使用parents函數

查找下一個兄弟使用next_sibling,查找上一個兄弟節點使用previous_sibling,如果是查找所有的,那么在對應的函數后面加s就可以

如果大家對Python感興趣的話,可以加一下我們的學習交流摳摳群哦:649825285,免費領取一套學習資料和視頻課程喲~

如何遍歷樹?

 使用find_all 函數

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

舉例說明:

print soup.find_all('title')
print soup.find_all('p','title')
print soup.find_all('a')
print soup.find_all(id="link2")
print soup.find_all(id=True)

返回值為:

[<title>The Dormouse's story</title>]
[<p class="title"><b>The Dormouse's story</b></p>]
[<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]
[<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>]
[<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]

通過css查找,直接上例子把:

print soup.find_all("a", class_="sister")
print soup.select("p.title")

通過屬性進行查找
print soup.find_all("a", attrs={"class": "sister"})

通過文本進行查找
print soup.find_all(text="Elsie")
print soup.find_all(text=["Tillie", "Elsie", "Lacie"])

限制結果個數
print soup.find_all("a", limit=2)

結果為:

[<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]
[<p class="title"><b>The Dormouse's story</b></p>]
[<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]
[u'Elsie']
[u'Elsie', u'Lacie', u'Tillie']
[<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>]

總之,通過這些函數可以查找到想要的東西。

總結

以上是生活随笔為你收集整理的python 中BeautifulSoup入门的全部內容,希望文章能夠幫你解決所遇到的問題。

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