Python下利用BeautifulSoup解析HTML的實現
摘要
Beautiful Soup 是一個可以從 HTML 或 XML 格式檔案中提取資料的 Python 庫,他可以將HTML 或 XML 資料解析為Python 物件,以方便通過Python程式碼進行處理。
文件環境
- Centos7.5
- Python2.7
- BeautifulSoup4
Beautifu Soup 使用說明
Beautiful Soup 的基本功能就是對HTML的標籤進行查詢及編輯。
基本概念-物件型別
Beautiful Soup 將複雜 HTML 文件轉換成一個複雜的樹形結構,每個節點都被轉換成一個Python 物件,Beautiful Soup將這些物件定義了4 種類型: Tag、NavigableString、BeautifulSoup、Comment 。
物件型別 | 描述 |
---|---|
BeautifulSoup | 文件的全部內容 |
Tag | HTML的標籤 |
NavigableString | 標籤包含的文字 |
Comment | 是一種特殊的NavigableString型別,當標籤中的NavigableString 被註釋時,則定義為該型別 |
安裝及引用
# Beautiful Soup pip install bs4 # 解析器 pip install lxml pip install html5lib
# 初始化 from bs4 import BeautifulSoup # 方法一,直接開啟檔案 soup = BeautifulSoup(open("index.html")) # 方法二,指定資料 resp = "<html>data</html>" soup = BeautifulSoup(resp,'lxml') # soup 為 BeautifulSoup 型別物件 print(type(soup))
標籤搜尋及過濾
基本方法
標籤搜尋有find_all() 和find() 兩個基本的搜尋方法,find_all() 方法會返回所有匹配關鍵字的標籤列表,find()方法則只返回一個匹配結果。
soup = BeautifulSoup(resp,'lxml') # 返回一個標籤名為"a"的Tag soup.find("a") # 返回所有tag 列表 soup.find_all("a") ## find_all方法可被簡寫 soup("a") #找出所有以b開頭的標籤 for tag in soup.find_all(re.compile("^b")): print(tag.name) #找出列表中的所有標籤 soup.find_all(["a","p"]) # 查詢標籤名為p,class屬性為"title" soup.find_all("p","title") # 查詢屬性id為"link2" soup.find_all(id="link2") # 查詢存在屬性id的 soup.find_all(id=True) # soup.find_all(href=re.compile("elsie"),id='link1') # soup.find_all(attrs={"data-foo": "value"}) #查詢標籤文字包含"sisters" soup.find(string=re.compile("sisters")) # 獲取指定數量的結果 soup.find_all("a",limit=2) # 自定義匹配方法 def has_class_but_no_id(tag): return tag.has_attr('class') and not tag.has_attr('id') soup.find_all(has_class_but_no_id) # 僅對屬性使用自定義匹配方法 def not_lacie(href): return href and not re.compile("lacie").search(href) soup.find_all(href=not_lacie) # 呼叫tag的 find_all() 方法時,Beautiful Soup會檢索當前tag的所有子孫節點,如果只想搜尋tag的直接子節點,可以使用引數 recursive=False soup.find_all("title",recursive=False)
擴充套件方法
ind_parents() | 所有父輩節點 |
find_parent() | 第一個父輩節點 |
find_next_siblings() | 之後的所有兄弟節點 |
find_next_sibling() | 之後的第一個兄弟節點 |
find_previous_siblings() | 之前的所有兄弟節點 |
find_previous_sibling() | 之前的第一個兄弟節點 |
find_all_next() | 之後的所有元素 |
find_next() | 之後的第一個元素 |
find_all_previous() | 之前的所有元素 |
find_previous() | 之前的第一個元素 |
CSS選擇器
Beautiful Soup支援大部分的CSS選擇器 http://www.w3.org/TR/CSS2/selector.html,在 Tag 或 BeautifulSoup 物件的 .select() 方法中傳入字串引數,即可使用CSS選擇器的語法找到tag。
html_doc = """ <html> <head> <title>The Dormouse's story</title> </head> <body> <p class="title">The Dormouse's story</p> <p class="story"> Once upon a time there were three little sisters; and their names were <a href="http://example.com/elsie" rel="external nofollow" class="sister" id="link1">Elsie</a>,<a href="http://example.com/lacie" rel="external nofollow" class="sister" id="link2">Lacie</a> and <a href="http://example.com/tillie" rel="external nofollow" class="sister" id="link3">Tillie</a>; and they lived at the bottom of a well. </p> <p class="story">...</p> """ soup = BeautifulSoup(html_doc) # 所有 a 標籤 soup.select("a") # 逐層查詢 soup.select("body a") soup.select("html head title") # tag標籤下的直接子標籤 soup.select("head > title") soup.select("p > #link1") # 所有匹配標籤之後的兄弟標籤 soup.select("#link1 ~ .sister") # 匹配標籤之後的第一個兄弟標籤 soup.select("#link1 + .sister") # 根據calss類名 soup.select(".sister") soup.select("[class~=sister]") # 根據ID查詢 soup.select("#link1") soup.select("a#link1") # 根據多個ID查詢 soup.select("#link1,#link2") # 根據屬性查詢 soup.select('a[href]') # 根據屬性值查詢 soup.select('a[href^="http://example.com/"]') soup.select('a[href$="tillie"]') soup.select('a[href*=".com/el"]') # 只獲取一個匹配結果 soup.select(".sister",limit=1) # 只獲取一個匹配結果 soup.select_one(".sister")
標籤物件方法
標籤屬性
soup = BeautifulSoup('<p class="body strikeout" id="1">Extremely bold</p><p class="body strikeout" id="2">Extremely bold2</p>') # 獲取所有的 p標籤物件 tags = soup.find_all("p") # 獲取第一個p標籤物件 tag = soup.p # 輸出標籤型別 type(tag) # 標籤名 tag.name # 標籤屬性 tag.attrs # 標籤屬性class 的值 tag['class'] # 標籤包含的文字內容,物件NavigableString 的內容 tag.string # 返回標籤內所有的文字內容 for string in tag.strings: print(repr(string)) # 返回標籤內所有的文字內容,並去掉空行 for string in tag.stripped_strings: print(repr(string)) # 獲取到tag中包含的所有及包括子孫tag中的NavigableString內容,並以Unicode字串格式輸出 tag.get_text() ## 以"|"分隔 tag.get_text("|") ## 以"|"分隔,不輸出空字元 tag.get_text("|",strip=True) 獲取子節點 tag.contents # 返回第一層子節點的列表 tag.children # 返回第一層子節點的listiterator 物件 for child in tag.children: print(child) tag.descendants # 遞迴返回所有子節點 for child in tag.descendants: print(child)
獲取父節點
tag.parent # 返回第一層父節點標籤 tag.parents # 遞迴得到元素的所有父輩節點 for parent in tag.parents: if parent is None: print(parent) else: print(parent.name)
獲取兄弟節點
# 下一個兄弟元素 tag.next_sibling # 當前標籤之後的所有兄弟元素 tag.next_siblings for sibling in tag.next_siblings: print(repr(sibling)) # 上一個兄弟元素 tag.previous_sibling # 當前標籤之前的所有兄弟元素 tag.previous_siblings for sibling in tag.previous_siblings: print(repr(sibling))
元素的遍歷
Beautiful Soup中把每個tag定義為一個“element”,每個“element”,被自上而下的在HTML中排列,可以通過遍歷命令逐個顯示標籤
# 當前標籤的下一個元素 tag.next_element # 當前標籤之後的所有元素 for element in tag.next_elements: print(repr(element)) # 當前標籤的前一個元素 tag.previous_element # 當前標籤之前的所有元素 for element in tag.previous_elements: print(repr(element))
修改標籤屬性
soup = BeautifulSoup('<b class="boldest">Extremely bold') tag = soup.b tag.name = "blockquote" tag['class'] = 'verybold' tag['id'] = 1 tag.string = "New link text." print(tag)
修改標籤內容(NavigableString)
soup = BeautifulSoup('<b class="boldest">Extremely bold') tag = soup.b tag.string = "New link text."
新增標籤內容(NavigableString)
soup = BeautifulSoup("<a>Foo</a>") tag = soup.a tag.append("Bar") tag.contents # 或者 new_string = NavigableString("Bar") tag.append(new_string) print(tag)
添加註釋(Comment)
註釋是一個特殊的NavigableString 物件,所以同樣可以通過append() 方法進行新增。
from bs4 import Comment soup = BeautifulSoup("<a>Foo</a>") new_comment = soup.new_string("Nice to see you.",Comment) tag.append(new_comment) print(tag)
新增標籤(Tag)
新增標籤方法有兩種,一種是在指定標籤的內部新增(append方法),另一種是在指定位置新增(insert、insert_before、insert_after方法)
append方法
soup = BeautifulSoup("") tag = soup.b new_tag = soup.new_tag("a",href="http://www.example.com" rel="external nofollow" ) new_tag.string = "Link text." tag.append(new_tag) print(tag)
* insert方法,是指在當前標籤子節點列表的指定位置插入物件(Tag或NavigableString)
html = '<a href="http://example.com/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >I linked to <i>example.com</i></a>' soup = BeautifulSoup(html) tag = soup.a tag.contents tag.insert(1,"but did not endorse ") tag.contents
insert_before() 和 insert_after() 方法則在當前標籤之前或之後的兄弟節點新增元素
html = '<a href="http://example.com/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >I linked to <i>example.com</i></a>' soup = BeautifulSoup(html) tag = soup.new_tag("i") tag.string = "Don't" soup.b.insert_before(tag) soup.b
* wrap() 和 unwrap()可以對指定的tag元素進行包裝或解包,並返回包裝後的結果。
```python # 新增包裝 soup = BeautifulSoup("<p>I wish I was bold.</p>") soup.p.string.wrap(soup.new_tag("b")) #輸出 I wish I was bold. soup.p.wrap(soup.new_tag("div")) #輸出 <div><p>I wish I was bold.</p></div> # 拆解包裝 markup = '<a href="http://example.com/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >I linked to <i>example.com</i></a>' soup = BeautifulSoup(markup) a_tag = soup.a a_tag.i.unwrap() a_tag #輸出 <a href="http://example.com/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >I linked to example.com</a>
刪除標籤
html = '<a href="http://example.com/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >I linked to <i>example.com</i></a>' soup = BeautifulSoup(html) # 清楚當前標籤的所有子節點 soup.b.clear() # 將當前標籤及所有子節點從soup 中移除,返回當前標籤。 b_tag=soup.b.extract() b_tag soup # 將當前標籤及所有子節點從soup 中移除,無返回。 soup.b.decompose() # 將當前標籤替換為指定的元素 tag=soup.i new_tag = soup.new_tag("p") new_tag.string = "Don't" tag.replace_with(new_tag)
其他方法
輸出
# 格式化輸出 tag.prettify() tag.prettify("latin-1")
- 使用Beautiful Soup解析後,文件都被轉換成了Unicode,特殊字元也被轉換為Unicode,如果將文件轉換成字串,Unicode編碼會被編碼成UTF-8.這樣就無法正確顯示HTML特殊字元了
- 使用Unicode時,Beautiful Soup還會智慧的把“引號”轉換成HTML或XML中的特殊字元
文件編碼
使用Beautiful Soup解析後,文件都被轉換成了Unicode,其使用了“編碼自動檢測”子庫來識別當前文件編碼並轉換成Unicode編碼。
soup = BeautifulSoup(html) soup.original_encoding # 也可以手動指定文件的編碼 soup = BeautifulSoup(html,from_encoding="iso-8859-8") soup.original_encoding # 為提高“編碼自動檢測”的檢測效率,也可以預先排除一些編碼 soup = BeautifulSoup(markup,exclude_encodings=["ISO-8859-7"]) 通過Beautiful Soup輸出文件時,不管輸入文件是什麼編碼方式,預設輸出編碼均為UTF-8編碼 文件解析器 Beautiful Soup目前支援,“lxml”,“html5lib”,和 “html.parser” soup=BeautifulSoup("<a><b /></a>") soup #輸出: <html><body><a></a></body></html> soup=BeautifulSoup("<a></p>","lxml") soup #輸出: <html><body><a></a></body></html> soup=BeautifulSoup("<a></p>","html5lib") soup #輸出: <html><head></head><body><a><p></p></a></body></html> soup=BeautifulSoup("<a></p>","html.parser") soup #輸出: <a></a>
參考文件
https://www.crummy.com/software/BeautifulSoup/bs4/doc.zh
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。