# Beautiful Soup 3
Beautiful Soup 3是上一個發布版本,目前已經停止維護.Beautiful Soup 3庫目前已經被幾個主要的linux平臺添加到源里:
`$ apt-get install Python-beautifulsoup`
在PyPi中分發的包名字是 `BeautifulSoup` :
`$ easy_install BeautifulSoup`
`$ pip install BeautifulSoup`
或通過 [Beautiful Soup 3.2.0源碼包](http://www.crummy.com/software/BeautifulSoup/bs3/download/3.x/BeautifulSoup-3.2.0.tar.gz) 安裝
Beautiful Soup 3的在線文檔查看 [這里](http://www.crummy.com/software/BeautifulSoup/bs3/documentation.html) ,當然還有 [中文版](http://www.crummy.com/software/BeautifulSoup/bs3/documentation.zh.html) ,然后再讀本片文檔,來對比Beautiful Soup 4中有什新變化.
## 遷移到BS4
只要一個小變動就能讓大部分的Beautiful Soup 3代碼使用Beautiful Soup 4的庫和方法—-修改 `BeautifulSoup` 對象的引入方式:
```
from BeautifulSoup import BeautifulSoup
```
修改為:
```
from bs4 import BeautifulSoup
```
* 如果代碼拋出 `ImportError` 異常“No module named BeautifulSoup”,原因可能是嘗試執行Beautiful Soup 3,但環境中只安裝了Beautiful Soup 4庫
* 如果代碼跑出 `ImportError` 異常“No module named bs4”,原因可能是嘗試運行Beautiful Soup 4的代碼,但環境中只安裝了Beautiful Soup 3.
雖然BS4兼容絕大部分BS3的功能,但BS3中的大部分方法已經不推薦使用了,就方法按照 [PEP8標準](http://www.Python.org/dev/peps/pep-0008/) 重新定義了方法名.很多方法都重新定義了方法名,但只有少數幾個方法沒有向下兼容.
上述內容就是BS3遷移到BS4的注意事項
### 需要的解析器
Beautiful Soup 3曾使用Python的 `SGMLParser` 解析器,這個模塊在Python3中已經被移除了.Beautiful Soup 4默認使用系統的 `html.parser` ,也可以使用lxml或html5lib擴展庫代替.查看 [安裝解析器](#id9) 章節
因為 `html.parser` 解析器與 `SGMLParser` 解析器不同,它們在處理格式不正確的文檔時也會產生不同結果.通常 `html.parser` 解析器會拋出異常.所以推薦安裝擴展庫作為解析器.有時 `html.parser` 解析出的文檔樹結構與 `SGMLParser` 的不同.如果發生這種情況,那么需要升級BS3來處理新的文檔樹.
### 方法名的變化
* `renderContents` -> `encode_contents`
* `replaceWith` -> `replace_with`
* `replaceWithChildren` -> `unwrap`
* `findAll` -> `find_all`
* `findAllNext` -> `find_all_next`
* `findAllPrevious` -> `find_all_previous`
* `findNext` -> `find_next`
* `findNextSibling` -> `find_next_sibling`
* `findNextSiblings` -> `find_next_siblings`
* `findParent` -> `find_parent`
* `findParents` -> `find_parents`
* `findPrevious` -> `find_previous`
* `findPreviousSibling` -> `find_previous_sibling`
* `findPreviousSiblings` -> `find_previous_siblings`
* `nextSibling` -> `next_sibling`
* `previousSibling` -> `previous_sibling`
Beautiful Soup構造方法的參數部分也有名字變化:
* `BeautifulSoup(parseOnlyThese=...)` -> `BeautifulSoup(parse_only=...)`
* `BeautifulSoup(fromEncoding=...)` -> `BeautifulSoup(from_encoding=...)`
為了適配Python3,修改了一個方法名:
* `Tag.has_key()` -> `Tag.has_attr()`
修改了一個屬性名,讓它看起來更專業點:
* `Tag.isSelfClosing` -> `Tag.is_empty_element`
修改了下面3個屬性的名字,以免雨Python保留字沖突.這些變動不是向下兼容的,如果在BS3中使用了這些屬性,那么在BS4中這些代碼無法執行.
* UnicodeDammit.Unicode -> UnicodeDammit.Unicode_markup``
* `Tag.next` -> `Tag.next_element`
* `Tag.previous` -> `Tag.previous_element`
### 生成器
將下列生成器按照PEP8標準重新命名,并轉換成對象的屬性:
* `childGenerator()` -> `children`
* `nextGenerator()` -> `next_elements`
* `nextSiblingGenerator()` -> `next_siblings`
* `previousGenerator()` -> `previous_elements`
* `previousSiblingGenerator()` -> `previous_siblings`
* `recursiveChildGenerator()` -> `descendants`
* `parentGenerator()` -> `parents`
所以遷移到BS4版本時要替換這些代碼:
```
for parent in tag.parentGenerator():
...
```
替換為:
```
for parent in tag.parents:
...
```
(兩種調用方法現在都能使用)
BS3中有的生成器循環結束后會返回 `None` 然后結束.這是個bug.新版生成器不再返回 `None` .
BS4中增加了2個新的生成器, [.strings 和 stripped_strings](#strings-stripped-strings) . `.strings` 生成器返回NavigableString對象, `.stripped_strings` 方法返回去除前后空白的Python的string對象.
### XML
BS4中移除了解析XML的 `BeautifulStoneSoup` 類.如果要解析一段XML文檔,使用 `BeautifulSoup` 構造方法并在第二個參數設置為“xml”.同時 `BeautifulSoup` 構造方法也不再識別 `isHTML` 參數.
Beautiful Soup處理XML空標簽的方法升級了.舊版本中解析XML時必須指明哪個標簽是空標簽. 構造方法的 `selfClosingTags` 參數已經不再使用.新版Beautiful Soup將所有空標簽解析為空元素,如果向空元素中添加子節點,那么這個元素就不再是空元素了.
### 實體
HTML或XML實體都會被解析成Unicode字符,Beautiful Soup 3版本中有很多處理實體的方法,在新版中都被移除了. `BeautifulSoup` 構造方法也不再接受 `smartQuotesTo` 或 `convertEntities` 參數. [編碼自動檢測](#unicode-dammit) 方法依然有 `smart_quotes_to` 參數,但是默認會將引號轉換成Unicode.內容配置項 `HTML_ENTITIES` , `XML_ENTITIES` 和 `XHTML_ENTITIES` 在新版中被移除.因為它們代表的特性已經不再被支持.
如果在輸出文檔時想把Unicode字符轉換成HTML實體,而不是輸出成UTF-8編碼,那就需要用到 [輸出格式](#id47) 的方法.
### 遷移雜項
[Tag.string](#string) 屬性現在是一個遞歸操作.如果A標簽只包含了一個B標簽,那么A標簽的.string屬性值與B標簽的.string屬性值相同.
[多值屬性](#id12) 比如 `class` 屬性包含一個他們的值的列表,而不是一個字符串.這可能會影響到如何按照CSS類名哦搜索tag.
如果使用 `find*` 方法時同時傳入了 [text 參數](#text) 和 [name 參數](#id32) .Beautiful Soup會搜索指定name的tag,并且這個tag的 [Tag.string](#string) 屬性包含text參數的內容.結果中不會包含字符串本身.舊版本中Beautiful Soup會忽略掉tag參數,只搜索text參數.
`BeautifulSoup` 構造方法不再支持 markupMassage 參數.現在由解析器負責文檔的解析正確性.
很少被用到的幾個解析器方法在新版中被移除,比如 `ICantBelieveItsBeautifulSoup` 和 `BeautifulSOAP` .現在由解析器完全負責如何解釋模糊不清的文檔標記.
`prettify()` 方法在新版中返回Unicode字符串,不再返回字節流.