偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

細(xì)說Beautiful Soup4,你學(xué)會了嗎?

開發(fā) 前端
Beautiful Soup 將復(fù)雜 HTML 文檔轉(zhuǎn)換成一個復(fù)雜的樹形結(jié)構(gòu),每個節(jié)點都是 Python 對象,所有對象可以歸納為 4 種。

Beautiful Soup4是一個 Python 庫,用于從 HTML 和 XML 文件中提取數(shù)據(jù)。它是一個工具箱,通過解析文檔為用戶提供需要抓取的數(shù)據(jù),Beautiful Soup自動將輸入文檔轉(zhuǎn)換為Unicode編碼,輸出文檔轉(zhuǎn)換為utf-8編碼。你不需要考慮編碼方式,除非文檔沒有指定一個編碼方式,這時,Beautiful Soup就不能自動識別編碼方式了。

BeautifulSoup安裝

使用pip來安裝BeautifulSoup。

pip install bs4 

另外要安裝解析器,下列表格列出一些常用的解析器。

圖片

使用BeautifulSoup及四大對象

1、創(chuàng)建BeautifulSoup對象

from bs4 import BeautifulSoup
import requests
url = "https://www.baidu.com"
content = requests.get(url).content
soup = BeautifulSoup(content)

print(soup.prettify()) // 格式化輸出
print(soup.get_text()) // 獲取網(wǎng)頁所有的文字內(nèi)容

2、BeautifulSoup四大對象

Beautiful Soup 將復(fù)雜 HTML 文檔轉(zhuǎn)換成一個復(fù)雜的樹形結(jié)構(gòu),每個節(jié)點都是 Python 對象,所有對象可以歸納為 4 種。

  • Tag:HTML中的標(biāo)簽,簡單來說就是html標(biāo)簽。
  • NavigableString:簡單來說就是標(biāo)簽里面的內(nèi)容,它的類型是一個NavigableString,翻譯過來叫可以遍歷的字符串。
  • BeautifulSoup:BeautifulSoup對象表示的是一個文檔的全部內(nèi)容,大部分時候,可以把它當(dāng)作Tag對象,是一個特殊的Tag,我們可以分別獲取它的類型、名稱、以及屬性
  • Comment:一個特殊類型的NavigableString對象,其實輸出的內(nèi)容不包括注釋符號

Tag對象示例

from bs4 import BeautifulSoup
import requests
url = "https://www.baidu.com"
content = requests.get(url).content
soup = BeautifulSoup(content)
print(soup.title)
print(soup.a)
print(soup.p)

運(yùn)行輸出如下圖所示,但是發(fā)現(xiàn)好像這個網(wǎng)頁不止一個a標(biāo)簽跟p標(biāo)簽,是因為它查找的是在所有內(nèi)容中的第一個符合要求的標(biāo)簽,要想得到所有符合要求的標(biāo)簽,后面會介紹find_all函數(shù)。

圖片

在Tag對象中有兩個重要的屬性,name和attrs。

import requests
url = "https://www.baidu.com"
content = requests.get(url).content
soup = BeautifulSoup(content)
print(soup.a.attrs)

運(yùn)行輸出如下圖所示,name輸出的是標(biāo)簽的本身,attrs輸出的是一個字典的類型,如果我們需要得到某個標(biāo)簽的某個屬性可以使用字典一些方法去獲取比如get方法,print(soup.p.get("class"))或者直接使用print(soup.p["class"])。

圖片

NavigableString代碼示例:

from bs4 import BeautifulSoup
import requests
url = "https://www.baidu.com"
content = requests.get(url).content
soup = BeautifulSoup(content)
print(soup.a.string)

運(yùn)行輸出如下圖,可以NavigableString類型的string方法輕松獲取到了標(biāo)簽里面的內(nèi)容。

圖片

BeautifulSoup代碼示例:

from bs4 import BeautifulSoup
import requests
url = "https://www.baidu.com"

content = requests.get(url).content
soup = BeautifulSoup(content)
print(soup.name)
print(soup.attrs)

運(yùn)行輸出如下圖所示:

圖片

Comment代碼示例:

from bs4 import BeautifulSoup
htmlText = '#<a class="sister" id="link1"><!-- Comment --></a>'
soup = BeautifulSoup(htmlText)
print(soup.a.string)

運(yùn)行輸出如下,a 標(biāo)簽里的內(nèi)容實際上是注釋,但是如果利用 .string方法來輸出它的內(nèi)容,發(fā)現(xiàn)它已經(jīng)把注釋符號去掉了,所以這可能會給帶來不必要的麻煩。

圖片

文檔樹遍歷

  • 直接子節(jié)點

tag里面的content屬性可以將tag的子節(jié)點以列表的形式返回。通過遍歷content.返回的列表來獲取每一個子節(jié)點或者直接使用tag的children方法來獲取。

from bs4 import BeautifulSoup
import requests
url = "https://www.baidu.com"
content = requests.get(url).content
soup = BeautifulSoup(content)
print(soup.head.contents)
for child in soup.head.contents:
print(child)

for child in soup.head.children:
print(child)

運(yùn)行輸出結(jié)果如下圖所示:

圖片

  • 所有子孫節(jié)點

tag里面的.descendants 屬性可以對所有tag的子孫節(jié)點進(jìn)行遞歸循環(huán),和 children類似,我們也需要遍歷獲取其中的內(nèi)容。

from bs4 import BeautifulSoup
import requests
url = "https://www.baidu.com"
content = requests.get(url).content
soup = BeautifulSoup(content)
for child in soup.descendants:
print(child)

運(yùn)行結(jié)果輸出如下圖所示:

圖片

  • 節(jié)點內(nèi)容

使用.string方法來獲取內(nèi)容,如果一個標(biāo)簽里面沒有標(biāo)簽了,那么 .string 就會返回標(biāo)簽里面的內(nèi)容。如果標(biāo)簽里面只有唯一的一個標(biāo)簽了,那么 .string 也會返回最里面的內(nèi)容,如果標(biāo)簽里面沒有內(nèi)容則返回None。

from bs4 import BeautifulSoup
import requests
url = "https://www.baidu.com"
content = requests.get(url).content
soup = BeautifulSoup(content)
print(soup.a.string)
print(soup.title.string)

運(yùn)行結(jié)果輸出如下圖所示:

圖片

  • 多個內(nèi)容

使用strippend_strings 屬性來獲取多個內(nèi)容還可以出除多余的空白字符,需要使用遍歷來獲取。

from bs4 import BeautifulSoup
import requests
url = "https://www.baidu.com"
content = requests.get(url).content
soup = BeautifulSoup(content)
for child in soup.stripped_strings:
print(child)

運(yùn)行結(jié)果輸出如下圖所示:

圖片

  • 父節(jié)點

通過元素的 .parents 屬性可以遞歸得到元素的所有父輩節(jié)點。

from bs4 import BeautifulSoup
import requests
url = "https://www.baidu.com"
content = requests.get(url).content
soup = BeautifulSoup(content,"html.parser")
parentObject = soup.head.title

for parent in parentObject.parent:
print(parent.name)

運(yùn)行結(jié)果輸出如下圖所示:

圖片

還有一些節(jié)點就不舉例,跟其它獲取節(jié)點一樣也是需要遍歷,而且使用的場景不同,兄弟節(jié)點使用.next_siblings或者.previous_sibling方法,前后節(jié)點使用.next_element或者.previous_element方法。

搜索文檔樹

find_all(name,attrs,recursive,text,\kwargs)**,find_all()方法用于搜索當(dāng)前tag的所有tag子節(jié)點,并判斷是否符合過濾條件。

  • 傳字符串

最簡單的過濾器是字符串,在搜索方法中傳入一個字符串參數(shù),beautifulsoup會查找與字符串完整匹配的內(nèi)容,下面的例子用于查找文檔中的所有a標(biāo)簽

from bs4 import BeautifulSoup
import requests
url = "https://www.baidu.com"
content = requests.get(url).content
soup = BeautifulSoup(content,"lxml")

print(soup.find_all("a"))

運(yùn)行結(jié)果如下圖所示:

圖片

  • 傳正則表達(dá)式

如果傳入正則表達(dá)式作為參數(shù),beautiful soup會通過正則表達(dá)式的match()來匹配內(nèi)容,下面例子中找出所有以b開頭的標(biāo)簽,這表示b開頭標(biāo)簽都應(yīng)該被找到。

from bs4 import BeautifulSoup
import requests
import re
url = "https://www.baidu.com"
content = requests.get(url).content
soup = BeautifulSoup(content,"lxml")

for tag in soup.find_all(re.compile('^b')):
print(tag.name)

運(yùn)行結(jié)果如下圖所示:

圖片

  • 傳列表

如果傳入列表參數(shù),Beautiful Soup會將與列表中任一元素匹配的內(nèi)容返回.下面代碼找到文檔中所有標(biāo)簽和標(biāo)簽。

from bs4 import BeautifulSoup
import requests

url = "https://www.baidu.com"
content = requests.get(url).content
soup = BeautifulSoup(content,"lxml")

print(soup.find_all(["a", "p"]))

運(yùn)行結(jié)果如下圖所示:

圖片

  • 傳True

true 可以匹配任何值,下面代碼查找到所有的tag,但是不會返回字符串節(jié)點。

from bs4 import BeautifulSoup
import requests

url = "https://www.baidu.com"
content = requests.get(url).content
soup = BeautifulSoup(content,"lxml")

for tag in soup.find_all(True):
print(tag.name)

運(yùn)行結(jié)果如下圖所示:

圖片

  • 傳函數(shù)

如果沒有合適過濾器,那么還可以定義一個函數(shù),函數(shù)只接受一個元素參數(shù),如果這個方法返回 True 表示當(dāng)前元素匹配并且被找到,如果不是則返回 False。

from bs4 import BeautifulSoup
import requests
url = "https://www.baidu.com"
content = requests.get(url).content
soup = BeautifulSoup(content,"lxml")
def has_class_but_no_id(tag):
return tag.has_attr('class') and not tag.has_attr('id')

print(soup.find_all(has_class_but_no_id))

輸出結(jié)果如下圖所示:

圖片

  • keyword 參數(shù)

注意:如果一個指定名字的參數(shù)不是搜索內(nèi)置的參數(shù)名,搜索時會把該參數(shù)當(dāng)作指定名字tag的屬性來搜索,如果包含一個名字為id的參數(shù),Beautifulsoup會搜索每個tag的'id'值。

import re
from bs4 import BeautifulSoup
import requests
url = "https://www.baidu.com"
content = requests.get(url).content
soup = BeautifulSoup(content,"lxml")
print(soup.find_all(id='lg'))
print(soup.find_all(href=re.compile("hao123")))

運(yùn)行結(jié)果如下圖所示:

圖片

find(name , attrs , recursive , text , **kwargs ), 它與 find_all() 方法唯一的區(qū)別是 find_all() 方法的返回結(jié)果是值包含一個元素的列表,而 find() 方法直接返回結(jié)果。?

CSS選擇器

在使用BeautifulSoup中常用的有5中css選擇器方法,用到的方法是 soup.select(),返回類型是列表。

  • 通過標(biāo)簽名查找
from bs4 import BeautifulSoup
import requests

url = "https://www.baidu.com"
content = requests.get(url).content
soup = BeautifulSoup(content,"lxml")
print(soup.select("title"))

運(yùn)行結(jié)果如下圖所示:

圖片

  • 通過CSS類名查找
from bs4 import BeautifulSoup
import requests

url = "https://www.baidu.com"
content = requests.get(url).content
soup = BeautifulSoup(content,"lxml")
print(soup.select(".mnav"))

運(yùn)行結(jié)果如下圖所示:

圖片

  • 通過ID來查找
from bs4 import BeautifulSoup
import requests

url = "https://www.baidu.com"
content = requests.get(url).content
soup = BeautifulSoup(content,"lxml")
print(soup.select("#lg"))

運(yùn)行結(jié)果如下圖所示:

圖片

  • 組合查找

組合查找有點類似前端CSS選擇器中的組合選擇器,組合查找還可以使用子代選擇器。

from bs4 import BeautifulSoup
import requests

url = "https://www.baidu.com"
content = requests.get(url).content
soup = BeautifulSoup(content,"lxml")
print(soup.select('div #lg'))

print(soup.select('div > a'))

運(yùn)行結(jié)果如下圖所示:

圖片

  • 通過CSS屬性查找

使用屬性需要用中括號括起來,注意屬性和標(biāo)簽屬于同一節(jié)點,所以中間不能加空格,否則會無法匹配到。

from bs4 import BeautifulSoup
import requests

url = "https://www.baidu.com"
content = requests.get(url).content
soup = BeautifulSoup(content,"lxml")
print(soup.select('a[class="mnav"]'))
  • 不同節(jié)點使用屬性查找
from bs4 import BeautifulSoup
import requests

url = "https://www.baidu.com"
content = requests.get(url).content
soup = BeautifulSoup(content,"lxml")
print(soup.select('span input[class="bg s_btn"]'))

運(yùn)行結(jié)果如下圖所示:

圖片

修改文檔樹

Beautiful Soup的強(qiáng)項是文檔樹的搜索,但同時也可以方便的修改文檔樹

  • 修改tag的名稱和屬性
from bs4 import BeautifulSoup
import requests
soup = BeautifulSoup('<b class="boldest">Extremely bold</b>',"lxml")
tag = soup.b
tag.name = "newtag"
tag['class'] = 'newclass'
tag['id'] = 1
print(tag)

del tag['class']
print(tag)

運(yùn)行結(jié)果如下圖所示:

圖片

  • 修改標(biāo)簽內(nèi)容

給tag的 .string 屬性賦值,就相當(dāng)于用當(dāng)前的內(nèi)容替代了原來的內(nèi)容,如果當(dāng)前的tag包含了其它tag,那么給它的 .string 屬性賦值會覆蓋掉原有的所有內(nèi)容包括子tag。

from bs4 import BeautifulSoup
import requests

markup = '<a >I linked to <i>example.com</i></a>'
soup = BeautifulSoup(markup,"lxml")

tag = soup.a
tag.string = "New link text."
print(tag)

運(yùn)行結(jié)果如下圖所示:

圖片

  • 在tag中添加內(nèi)容

Tag.append() 方法可以在tag中添加內(nèi)容。

from bs4 import BeautifulSoup
import requests

soup = BeautifulSoup("<a>Foo</a>","lxml")
soup.a.append("Bar")
print(soup)
print(soup.a.contents)

運(yùn)行結(jié)果如下圖所示:

圖片

總結(jié)

本篇內(nèi)容比較多,把 Beautiful Soup 的方法進(jìn)行了大部分整理和總結(jié),但是還不夠完整只是列出一些常用的,如果需要完整的可以查看Beautiful Soup 官網(wǎng)的文檔,希望對大家有幫助,掌握了 Beautiful Soup,一定會給你在數(shù)據(jù)爬取帶來方便。

本文轉(zhuǎn)載自微信公眾號「愛編碼的社畜」,可以通過以下二維碼關(guān)注。轉(zhuǎn)載本文請聯(lián)系愛編碼的社畜公眾號。

責(zé)任編輯:姜華 來源: 愛編碼的社畜
相關(guān)推薦

2024-01-19 08:25:38

死鎖Java通信

2024-02-04 00:00:00

Effect數(shù)據(jù)組件

2023-07-26 13:11:21

ChatGPT平臺工具

2023-01-10 08:43:15

定義DDD架構(gòu)

2023-08-01 12:51:18

WebGPT機(jī)器學(xué)習(xí)模型

2024-01-02 12:05:26

Java并發(fā)編程

2023-10-10 11:04:11

Rust難點內(nèi)存

2024-05-06 00:00:00

InnoDBView隔離

2024-07-31 08:39:45

Git命令暫存區(qū)

2023-01-30 09:01:54

圖表指南圖形化

2022-07-08 09:27:48

CSSIFC模型

2023-12-12 08:02:10

2024-08-06 09:47:57

2024-03-06 08:28:16

設(shè)計模式Java

2022-06-16 07:50:35

數(shù)據(jù)結(jié)構(gòu)鏈表

2022-12-06 07:53:33

MySQL索引B+樹

2023-01-31 08:02:18

2023-10-06 14:49:21

SentinelHystrixtimeout

2022-07-13 08:16:49

RocketMQRPC日志

2023-05-05 06:54:07

MySQL數(shù)據(jù)查詢
點贊
收藏

51CTO技術(shù)棧公眾號