2017-05-14 1 views
0

Ich bin Scraping der python.org Website für einige Informationen mit beautifulsoup. Ich versuche auch, das Programm zu bekommen den Rückgabetyp einer Funktion istnameerror während der Verwendung von eval mit beautifulsoup

Mein Code zu drucken, wie folgt:

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__ 

Ich versuche, die Funktion im String-Format und weitergeben abrufen Evaluierungs- und bekommen die Rückkehr Info .__doc__

die in diesem Fall ist string.capwords

jedoch verwenden, bekomme ich folgende Fehlermeldung:

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 

Antwort

1

Sie benötigen string auf

import string 

Dann

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

Dank zu importieren. Gibt es eine Alternative? –

+1

ohne Import-String? 'getattr (import_module (func_name.split (". ") [0]), func_name.split (". ") [1]) .__ doc__' zu benutzen' von importlib import import_module ' – itzMEonTV

+0

Danke das ist, was ich wollte. Irgendeine Idee, wie man Arten von Eingabeparametern erhält? –

Verwandte Themen