Beautiful Soup 简明教程

Beautiful Soup - Porting Old Code

你可以对来自 Beautiful Soup 的早期版本中的代码进行更改,使之与最新版本兼容,在导入语句中进行以下更改 -

Example

from BeautifulSoup import BeautifulSoup
#becomes this:

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。

Beautiful Soup 3 使用了 Python 的 SGMLParser,这是一个在 Python 3.0 中已被删除的模块。Beautiful Soup 4 默认使用 html.parser,但是你也可以使用 lxml 或 html5lib。

尽管 BS4 基本上与 BS3 向后兼容,但它的很多方法已被弃用且赋予了新的名称,以便符合 PEP 8 合规要求。

这里有一些示例 -

replaceWith -> replace_with
findAll -> find_all
findNext -> find_next
findParent -> find_parent
findParents -> find_parents
findPrevious -> find_previous
getText -> get_text
nextSibling -> next_sibling
previousSibling -> previous_sibling