我看到答案,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()
或漂亮的匹配任何东西,取决于网页。
谢谢,这就是解决方案!如你所说,必须考虑更多与安全相关的后果。 – MJo 2013-03-31 19:31:42