我看到 getattr() 用于一些简单的方法/函数调用的答案。
任意字符串怎么样,例如在这里进行网络解析:
from bs4 import BeautifulSoup
import urllib
f = urllib.urlopen(link) # link comes from database, e.g. 'http://www.example.com'
soup = BeautifulSoup(f)
text = soup.find(True, 'text').get_text() # Now this is hardcoded
工作正常,但是运行来自数据库的解析器字符串怎么样?字符串可以是:
soup.find("div", "layout left").find(id=True).get_text()
或完全匹配任何东西,取决于网页。