bs4 文件夹中没有 beautifulsoup.py 文件?

No beautifulsoup.py file in bs4 folder?

我想看看 beautifulsoup.py 中的内容,就像在 parse.py

中一样

我安装了bs4, but no beautifulsoup.py file?我希望在 "bs4" 文件夹中看到一个 "beautifulsoup.py",因为导入它是:from bs4 import BeautifulSoup.

导入 parse 是:from urllib import parse 我看到 parse.py here。美汤怎么样?

bs4.BeautifulSoup 不是子模块,它是 class。您可以在 bs4/__init__.py.

中找到它的定义

你可以看到它是一个像这样的class:

>>> from bs4 import BeautifulSoup
>>> type(BeautifulSoup)
type

... 你可以使用 inspect 模块中的函数查看它来自哪个文件,例如:

>>> import inspect
>>> inspect.getfile(BeautifulSoup)
'/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/bs4/__init__.py'

语法 from X import Y 可以做两种不同的事情:

  • 如果 Y 是 class、函数或 X 中的其他名称,它会将模块 X 中的值作为 Y 导入到全局变量中].
  • 如果 Y 是包 X 下的子模块,它会将该子模块作为模块 Y.
  • 导入到全局变量中

有关完整详细信息,请参阅文档中的 The import system

The from form uses a slightly more complex process:

  1. find the module specified in the from clause, loading and initializing it if necessary;
  2. for each of the identifiers specified in the import clauses:
    1. check if the imported module has an attribute by that name
    2. if not, attempt to import a submodule with that name and then check the imported module again for that attribute
    3. if the attribute is not found, ImportError is raised.
    4. otherwise, a reference to that value is stored in the local namespace, using the name in the as clause if it is present, otherwise using the attribute name