Python 将eval与beautifulsoup一起使用时发生nameerror

Python 将eval与beautifulsoup一起使用时发生nameerror,python,beautifulsoup,eval,Python,Beautifulsoup,Eval,我正在使用beautifulsoup从python.org网站上获取一些信息。我还试图让程序打印函数的返回类型 我的代码如下: soup = Soup(gethtml('https://docs.python.org/3/library/string.html')) for function in soup.find_all('dl', {'class': 'function'}): try: func_name = function.dt['id'] p

我正在使用beautifulsoup从python.org网站上获取一些信息。我还试图让程序打印函数的返回类型

我的代码如下:

soup = Soup(gethtml('https://docs.python.org/3/library/string.html'))
for function in soup.find_all('dl', {'class': 'function'}):
    try:
        func_name = function.dt['id']
        print eval(func_name).__doc__
我正在尝试以字符串格式检索函数,并将其传递给eval,并使用
\uuuu doc\uuuu

在本例中是
string.capwords

但是,我得到以下错误:

Traceback (most recent call last):
  File "C:/Users/GX70/PycharmProjects/assignment/tasks/libscrape.py", line 58, in <module>
    print eval(func_name).__doc__
  File "<string>", line 1, in <module>
NameError: name 'string' is not defined
回溯(最近一次呼叫最后一次):
文件“C:/Users/GX70/PycharmProjects/assignment/tasks/libscrape.py”,第58行,在
打印评估(职能名称)。\单据__
文件“”,第1行,在
名称错误:未定义名称“字符串”

您需要导入顶部的
字符串

import string
然后


谢谢有没有其他选择?没有导入字符串
getattr(import_module(func_name.split(“.”[0]),func_name.split(“.”[1])。\uuuuu doc_uu
使用importlib import_module的
,谢谢,这就是我想要的。知道如何获取输入参数的类型吗?
In [165]: eval("string.capwords.__doc__")
Out[165]: 'capwords(s [,sep]) -> string\n\n    Split the argument into words using split, capitalize each\n    word using capitalize, and join the capitalized words using\n    join.  If the optional second argument sep is absent or None,\n    runs of whitespace characters are replaced by a single space\n    and leading and trailing whitespace are removed, otherwise\n    sep is used to split and join the words.\n\n    '