Python爬蟲之bs4庫

python爬蟲常用庫之bs4


bs4全名BeautifulSoup,是編寫python爬蟲常用庫之一,主要用來解析html標籤。


1.安裝

pip install beautifulsoup4

python -m pip install beautifulsoup4

2.基本使用方法

bs4中最基礎的使用是BeautifulSoup類的使用,注意大小寫哦。

用BeautifulSoup來解析html:

from bs4 import BeautifulSoup
soup1 = BeautifulSoup("<html> A Html Text</html>", "html.parser")
soup2 = BeautifulSoup(open("d://demo.html"), "html.parser")
兩個參數:第一個參數是要解析的html文本,第二個參數是使用那種解析器,對於HTML來講就是html.parser,這個是bs4自帶的解析器。 還可以安裝lxml庫來解析HTML或者XML,安裝html5lib來解析html5lib。

#lxml解析html(需pip install lxml)
BeautifulSoup(html,'lxml')
#lxml解析XML
BeautifulSoup(xml,'xml')
#html5lib解析(需安裝: pip install html5lib)
BeautifulSoup(html5,'html5lib')

2.1 BeautifulSoup基本元素

BeautifulSoup基本元素有:

基本元素 說明
Tag 標籤,基本信息組織單元,用<>和</>標明開頭和結尾
Name 標籤的名字,<p>...</p>的名字就是’p',用法<>.name
Attributes 標籤屬性,字典形式,用法:<>.attrs['href']
NavigableString 標籤內非屬性字符串,<p>這是非屬性字符串</p>,用法:<>.string
Comment 標籤內的註釋部分<p>顯示的內容<!-- comment就是這裏啦--></p>
2.1.1 Tag標籤

任何存在於html語法中的標籤都可以用soup.<tag>訪問獲得。

當HTML文檔中存在多個相同的tag時,soup.<tag>返回第一個

>>> soup2 = BeautifulSoup("<p class=\"title\"><b>The Contents of b in first p</b></p><p class=\"course\">The second p</p>","html.parser")
>>> soup2.p
<p class="title"><b>The Contents of b in first p</b></p>

2.1.2 Tag的name

每個Tag都有自己的名字,通過<tag>.name獲取,字符串類型

>>> soup2.p.name
'p'
2.1.3 Tag的attrs(屬性)

一個Tag可以有0個或多個屬性,字典類型。

>>> soup2.p.attrs
{'class': ['title']}
>>> soup2.p.attrs['class']
['title']

2.1.4 Tag的NavigableString

NavigableString可以跨越多個層次的標籤。

>>> soup2.p
<p class="title"><b>The Contents of b in first p</b></p>
>>> soup2.p.string
'The Contents of b in first p'
>>> 

2.1.5 Tag 的Comment

Comment是一種特殊類型

>>> soup3 = BeautifulSoup("<p>This is a NavigableString</p><b><!-- This is a Comment --></b>","html.parser")
>>> soup3.p
<p>This is a NavigableString</p>
>>> soup3.b
<b><!-- This is a Comment --></b>
>>> soup3.p.string
'This is a NavigableString'
>>> type(soup3.p.string)
<class 'bs4.element.NavigableString'>
>>> soup3.b
<b><!-- This is a Comment --></b>
>>> soup3.b.string
' This is a Comment '
>>> type(soup3.b.string)
<class 'bs4.element.Comment'>
>>> 


<p class="title">....</p>
p ->tag.name  'p'

class="title" -> tag.attrs (字典列表)

... -> NavigableString OR Comment

2.2 使用bs4遍歷html內容

HTML是個樹狀結構,<>...</>構成了從屬關係。對HTML的遍歷,有下行遍歷,上行遍歷和平行遍歷三種遍歷途徑或方法。

2.2.1 下行遍歷

屬性 說明
.contents 子節點的列表,將<tag>所有的兒子節點存入列表
.children 子節點的迭代類型,與.contents類似,主要用於循環遍歷子節點
.descendants 子孫節點的迭代類型,包含所有子孫節點,用於循環遍歷。

BeautifulSoup類型是標籤樹的根節點。

soup.head
soup.head.contents
soup.body.contents
len(soup.body.contents)
soup.body.contents[0]
遍歷子節點:
for child in soup.body.children:
    print(child)
遍歷所有子孫節點:

for child in soup.body.descendants:
    print(child)

2.2.2 上行遍歷

屬性 說明
.parent 節點的父節點
.parents 節點的先輩節點標籤的迭代類型,用於循環遍歷先輩節點。

>>> soup
<html><head><title>This is a python demo page</title></head>
<body>
<p class="title"><b>The demo python introduces several python courses.</b></p>
<p class="course">Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:

<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a> and <a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">Advanced Python</a>.</p>
</body></html>
>>> soup.title.parent
<head><title>This is a python demo page</title></head>
>>> soup.html.parent
<html><head><title>This is a python demo page</title></head>
<body>
<p class="title"><b>The demo python introduces several python courses.</b></p>
<p class="course">Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:

<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a> and <a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">Advanced Python</a>.</p>
</body></html>
>>> soup.parent
>>> 
注意:<html>..</html>標籤的父節點是其自身

而soup本身的父節點是空。

進行先輩節點遍歷時,包括soup自身,實際使用時需要判斷。

for parent in soup.a.parents:
	if parent is None:
		print(parent)
	else:
		print(parent.name)

2.2.3 平行遍歷

屬性 說明
.next_sibling 返回按照HTML文本順序的下一個平行節點標籤
.previous_sibling 返回按照HTML文本順序的上一個平行節點標籤
.next_siblings 迭代類型,返回按照HTML文本順序的後續所有平行節點標籤
.previous_siblings 迭代類型,返回按照HTML文本順序的前續所有平行節點標籤
平行遍歷發生在同一個父節點下的各節點之間。

soup.a.next_sibling
soup.a.next_sibling.nextsibling
soup.a.previous_sibling
soup.a.parent

遍歷後續節點:

for sibling in soup.a.next_siblings:
	print(sibling)

遍歷前續節點:

for sibling in previous_siblings:
	print(sibling)

3. 基於bs4庫的HTML格式輸出

3.1 prettify()方法

.prettify()爲HTML文本<>及其內容增加‘\n'

.prettify()可用於標籤,方法<tag>.prettify()

print(soup.a.prettify())

3.2 bs4k庫的編碼

bs4庫將任何HTML輸入都變成utf-8編碼,python3.x 默認支持編碼是utf-8。完美匹配!


4.使用bs4進行HTML內容查找

使用bs4進行HTML內容解析查找,基本方法是使用<>.find_all()來進行

4.1 .find_all()的基本使用方法

基本格式:

<tag>.find_all(name, attrs, recursive, string, **kwargs)

其返回值爲一個列表,存儲查找的結果

參數:

name -> 對標籤名稱的檢索字符串,可以是個字符串列表,表達“或”關係

soup.find_all('a')
soup.find_all(['a','b'])
soup.find_all(True)


attrs -> 對標籤屬性值的檢索字符串,可標註屬性檢索

soup.find_all('a', 'title')
soup.find_all(id='link1')
soup.find_all(attrs = {"class":"course"})

recursive -> 是否對子孫全部檢索,默認True

string -> <>...</>中的....的檢索字符串

soup.find_all(string = 'This is a sample')


因爲find_all太常用了,所以有簡略用法:

<tag>(...) <--> <tag>.find_all(...)

soup(...) <--> soup.find_all(...)

程序員果然都是懶人..........大笑

4.2 擴展方法

方法 說明
<>.find() 搜索且只返回一個結果,同.find_all()參數
<>.find_parents() 在先輩節點中搜索,返回列表類型,同.find_all()參數
<>.find_parent() 在先輩節點中返回一個結果,同.find()參數
<>.find_next_siblings() 在後續平行節點中搜索,返回列表類型,同find_all()參數
<>.find_next_sibling() 在後續平行節點中搜索,返回一個結果,同find()參數
<>.find_previous_siblings() 在前續平行節點中搜索,返回列表類型,同find_all()參數
<>.find_previous_sibling() 在前續平行節點中搜索,返回一個結果,同find()參數

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章