我正在尝试创建一个带有多个参数的函数,并返回一个可调用的lambda函数.我将这些lambda函数传递给BeautifulSoup的find_all方法以解析html.
这是我为生成lambda函数而编写的函数:
def tag_filter_function(self, name="", search_terms={}, attrs=[], **kwargs):
# filter attrs that are in the search_terms keys out of attrs
attrs = [attr for attr in attrs if attr not in search_terms.keys()]
# array of strings to compile into a lambda function
exec_strings = []
# add name search into exec_strings
if len(name) > 0:
tag_search_name = "tag.name == \"{}\"".format(name)
exec_strings.append(tag_search_name)
# add generic search terms into exec_strings
if len(search_terms) > 0:
tag_search_terms = ' and '.join(["tag.has_attr(\"{}\") and …Run Code Online (Sandbox Code Playgroud)