2017-05-14 8 views
0

私は、python.orgのウェブサイトをいくつかの情報について拝みました。beautifulsoupbeautifulsoupでevalを使用しているときにエラーが発生する

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に渡すと、取得しています:私はまた

は、次のように私のコードがある

関数の戻り値の種類を印刷するプログラムを取得しようとしています

:このケースでは、私は次のエラーを取得する、しかし string.capwords

ある.__doc__

を使用して、戻り情報あなたがトップ

import string 

stringをインポートする必要が

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 

答えて

1

その後

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 ' 
+0

感謝。代わりがありますか? –

+1

インポート文字列なし? 'getattr(import_module(func_name.split("。 ")[0])、func_name.split("。 "[1]).__ doc__から' from importlib import import_module ' – itzMEonTV

+0

ありがとうございました。任意のアイデア入力パラメータの種類を取得する方法? –

関連する問題