成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

細說Beautiful Soup4,你學會了嗎?

開發 前端
Beautiful Soup 將復雜 HTML 文檔轉換成一個復雜的樹形結構,每個節點都是 Python 對象,所有對象可以歸納為 4 種。

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

BeautifulSoup安裝

使用pip來安裝BeautifulSoup。

pip install bs4 

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

圖片

使用BeautifulSoup及四大對象

1、創建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()) // 獲取網頁所有的文字內容

2、BeautifulSoup四大對象

Beautiful Soup 將復雜 HTML 文檔轉換成一個復雜的樹形結構,每個節點都是 Python 對象,所有對象可以歸納為 4 種。

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

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)

運行輸出如下圖所示,但是發現好像這個網頁不止一個a標簽跟p標簽,是因為它查找的是在所有內容中的第一個符合要求的標簽,要想得到所有符合要求的標簽,后面會介紹find_all函數。

圖片

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

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

運行輸出如下圖所示,name輸出的是標簽的本身,attrs輸出的是一個字典的類型,如果我們需要得到某個標簽的某個屬性可以使用字典一些方法去獲取比如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)

運行輸出如下圖,可以NavigableString類型的string方法輕松獲取到了標簽里面的內容。

圖片

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)

運行輸出如下圖所示:

圖片

Comment代碼示例:

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

運行輸出如下,a 標簽里的內容實際上是注釋,但是如果利用 .string方法來輸出它的內容,發現它已經把注釋符號去掉了,所以這可能會給帶來不必要的麻煩。

圖片

文檔樹遍歷

  • 直接子節點

tag里面的content屬性可以將tag的子節點以列表的形式返回。通過遍歷content.返回的列表來獲取每一個子節點或者直接使用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)

運行輸出結果如下圖所示:

圖片

  • 所有子孫節點

tag里面的.descendants 屬性可以對所有tag的子孫節點進行遞歸循環,和 children類似,我們也需要遍歷獲取其中的內容。

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)

運行結果輸出如下圖所示:

圖片

  • 節點內容

使用.string方法來獲取內容,如果一個標簽里面沒有標簽了,那么 .string 就會返回標簽里面的內容。如果標簽里面只有唯一的一個標簽了,那么 .string 也會返回最里面的內容,如果標簽里面沒有內容則返回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)

運行結果輸出如下圖所示:

圖片

  • 多個內容

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

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)

運行結果輸出如下圖所示:

圖片

  • 父節點

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

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)

運行結果輸出如下圖所示:

圖片

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

搜索文檔樹

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

  • 傳字符串

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

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"))

運行結果如下圖所示:

圖片

  • 傳正則表達式

如果傳入正則表達式作為參數,beautiful soup會通過正則表達式的match()來匹配內容,下面例子中找出所有以b開頭的標簽,這表示b開頭標簽都應該被找到。

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)

運行結果如下圖所示:

圖片

  • 傳列表

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

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"]))

運行結果如下圖所示:

圖片

  • 傳True

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

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)

運行結果如下圖所示:

圖片

  • 傳函數

如果沒有合適過濾器,那么還可以定義一個函數,函數只接受一個元素參數,如果這個方法返回 True 表示當前元素匹配并且被找到,如果不是則返回 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))

輸出結果如下圖所示:

圖片

  • keyword 參數

注意:如果一個指定名字的參數不是搜索內置的參數名,搜索時會把該參數當作指定名字tag的屬性來搜索,如果包含一個名字為id的參數,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")))

運行結果如下圖所示:

圖片

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

CSS選擇器

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

  • 通過標簽名查找
from bs4 import BeautifulSoup
import requests

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

運行結果如下圖所示:

圖片

  • 通過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"))

運行結果如下圖所示:

圖片

  • 通過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"))

運行結果如下圖所示:

圖片

  • 組合查找

組合查找有點類似前端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'))

運行結果如下圖所示:

圖片

  • 通過CSS屬性查找

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

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"]'))
  • 不同節點使用屬性查找
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"]'))

運行結果如下圖所示:

圖片

修改文檔樹

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

  • 修改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)

運行結果如下圖所示:

圖片

  • 修改標簽內容

給tag的 .string 屬性賦值,就相當于用當前的內容替代了原來的內容,如果當前的tag包含了其它tag,那么給它的 .string 屬性賦值會覆蓋掉原有的所有內容包括子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)

運行結果如下圖所示:

圖片

  • 在tag中添加內容

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

from bs4 import BeautifulSoup
import requests

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

運行結果如下圖所示:

圖片

總結

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

本文轉載自微信公眾號「愛編碼的社畜」,可以通過以下二維碼關注。轉載本文請聯系愛編碼的社畜公眾號。

責任編輯:姜華 來源: 愛編碼的社畜
相關推薦

2024-01-19 08:25:38

死鎖Java通信

2024-02-04 00:00:00

Effect數據組件

2023-07-26 13:11:21

ChatGPT平臺工具

2023-01-10 08:43:15

定義DDD架構

2023-08-01 12:51:18

WebGPT機器學習模型

2024-01-02 12:05:26

Java并發編程

2023-10-10 11:04:11

Rust難點內存

2024-05-06 00:00:00

InnoDBView隔離

2024-07-31 08:39:45

Git命令暫存區

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

設計模式Java

2022-06-16 07:50:35

數據結構鏈表

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數據查詢
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 超黄视频网站 | 中文在线一区二区 | 亚洲日本欧美日韩高观看 | 国产欧美日韩精品在线观看 | 自拍亚洲 | 免费一区二区三区 | 韩国成人在线视频 | 91综合在线观看 | 国产日韩一区二区三免费高清 | 午夜久久久 | 日韩在线资源 | 在线成人www免费观看视频 | 天天久久 | av入口 | 欧美美女爱爱视频 | 国产亚洲一级 | 欧美一区久久 | 黄色三级毛片 | 久久亚洲一区二区三 | 久久精品69 | 久久久久久久久久久丰满 | 国产69久久精品成人看动漫 | 日日操天天射 | 午夜精品久久久久99蜜 | 欧美jizzhd精品欧美巨大免费 | 亚洲一区中文 | 久久精品国产久精国产 | 国产乱码精品一区二区三区五月婷 | 91极品欧美视频 | 国产一区二区 | 国产成人福利在线观看 | 国产精品久久久久久久久久久久久 | 欧美日韩中文字幕在线 | 亚洲欧美日韩精品久久亚洲区 | 日韩免费一区二区 | 伊人网站 | 国产高潮好爽受不了了夜色 | 国产一区二区久久 | 在线免费观看a级片 | 天天插天天射天天干 | 伊人春色在线 |