Beautiful Soup 简明教程
Beautiful Soup - Porting Old Code
你可以对来自 Beautiful Soup 的早期版本中的代码进行更改,使之与最新版本兼容,在导入语句中进行以下更改 -
You can make the code from earlier version of Beautiful Soup compatible with the lates version by making following change in the import statement −
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。
If you get the ImportError "No module named BeautifulSoup", it means you’re trying to run Beautiful Soup 3 code, but you only have Beautiful Soup 4 installed. Similarly, If you get the ImportError "No module named bs4", because you’re trying to run Beautiful Soup 4 code, but you only have Beautiful Soup 3 installed.
Beautiful Soup 3 使用了 Python 的 SGMLParser,这是一个在 Python 3.0 中已被删除的模块。Beautiful Soup 4 默认使用 html.parser,但是你也可以使用 lxml 或 html5lib。
Beautiful Soup 3 used Python’s SGMLParser, a module that has been removed in Python 3.0. Beautiful Soup 4 uses html.parser by default, but you can also use lxml or html5lib.
尽管 BS4 基本上与 BS3 向后兼容,但它的很多方法已被弃用且赋予了新的名称,以便符合 PEP 8 合规要求。
Although BS4 is mostly backwards-compatible with BS3, most of its methods have been deprecated and given new names for PEP 8 compliance.
这里有一些示例 -
Here are a few examples −
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