1. 程式人生 > >爬蟲-基於bs4庫的HTML內容查找方法

爬蟲-基於bs4庫的HTML內容查找方法

簽名 正則化 all 擴展 rev recursive title 參數 pre

bs4有一個find_all(name,attrs,recursive,string,**kwargs)方法,返回一個列表類型,存儲查找的結果

name 對標簽名稱的檢索字符串

attrs 對標簽屬性值的檢索字符串,可標註屬性檢索,可查找某標簽中是否含有特定的字符串

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

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

舉例說明:

name

soup.find_all(a)#返回a標簽的內容
soup.find_all([a,b])#返回a和b標簽的內容

for tag in soup.find_all(True):#
打印文檔中的所有標簽名字 print(tag.name) ‘‘‘ 返回 html head title body p b p a a ‘‘‘ #使用正則化後: import re#如果我們只想得到以b開頭的標簽,n那麽我們需要正則表達式,re是相應的庫 for tag in soup.find_all(re.compile(b)): print(tag.name) #返回 body b

attrs:

soup.find_all(p,course)#查找p標簽中包含‘course‘的信息

soup.find_all(id=link1)
‘‘‘返回 [<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a>]
‘‘‘ soup.find_all(link)#返回[] import re soup.find_all(id=re.compile(link))#利用正則表達式查找包含link的標簽內容 ‘‘‘ [<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a>, <a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">Advanced Python</a>]
‘‘‘

recursive:

soup.find_all(a,recursive=False)
#返回[]表明兒子節點上沒有a標簽

string:

soup.find_all(string=Basic Python)
#[‘Basic Python‘]

import re
soup.find_all(string=re.compile(python))#所有在字符串中出現Python的字符串檢索
‘‘‘
[‘This is a python demo page‘,
 ‘The demo python introduces several python courses.‘]
‘‘‘

另外,我們可以使用

<tag>(..)等價於<tag>.find_all(..)

soup(..)等價於soup.find_all(..)

find的擴展方法

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

爬蟲-基於bs4庫的HTML內容查找方法