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

2024-09-25 00:36:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我想看看里面的东西美丽组合.py就像在parse.py

我安装了bs4,但没有beautifulsoup.pyfile?我希望看到一个美丽组合.py在“bs4”文件夹中,因为导入它是:from bs4 import BeautifulSoup。你知道吗

导入parse是:from urllib import parse,我看到一个解析.pyhere。靓汤怎么样?你知道吗


Tags: frompyimport文件夹parseurllibpyfilebs4
1条回答
网友
1楼 · 发布于 2024-09-25 00:36:32

bs4.BeautifulSoup不是子模块,而是类。你可以在^{}中找到它的定义。你知道吗


你可以看到它是这样一个类:

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

…您可以使用^{}模块中的函数看到它来自哪个文件,例如:

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

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

  • 如果YX中的类、函数或其他名称,它将该值作为Y从模块X导入全局变量。你知道吗
  • 如果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

相关问题 更多 >