我正在 HTML 源代码中搜索具有 "up"
或 "down"
的行。只需要一个字,代码就可以工作。但是如果我想搜索包含多个单词的行,如何在 Python 3.4.2 中做到这一点?
links = lambda tag: getattr(tag, 'name', None) == 'a' and 'up|down' in tag.get_text().lower() and 'href' in tag.attrs
最佳答案
您始终可以创建常规函数,而不是简短的 lambda
形式。
定义要在标签
文本中检查的单词列表并使用 any()
+ in
检查文本中是否存在单词:
stop_words = ['up', 'down']
def filter_links(tag):
if getattr(tag, 'name', None) == 'a' and 'href' in tag.attrs:
text = tag.get_text().lower()
return any(item in text for item in stop_words)
return False
关于python - 如何使用 lambda 搜索多个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27740158/