我看到答案,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