python - 使用 BeautifulSoup 检查列表中的字符串是否存在于 HTML 中

标签 python beautifulsoup

我使用以下代码在已解析的 HTML 中查找文本:

searched_word = "News"
results = parsedHTML.body.find_all(string=re.compile('.*{0}.*'.format(searched_word)), recursive=True)
if results:
    doStuff()

这可行,但我想使用列表,例如:

searched_words = ["News", "Team"]

如果我解析的 HTML 内容中包含任何这些字符串元素,则应返回 True 以及在 HTML 中找到的元素。我不知道如何实现这一点。

最佳答案

这可能会有所帮助。

searched_words = ["News", "Team"]
pattern = re.compile("|".join(searched_words))
results = parsedHTML.body.find_all(string=pattern, recursive=True)
if results:
    doStuff()

关于python - 使用 BeautifulSoup 检查列表中的字符串是否存在于 HTML 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56684991/

相关文章:

python - 遍历列表,比较值并删除重复项 - Python

python - find_all() 仅返回列表的第一项

python - 使用 BeautifulSoup 删除 td 元素之间/之后的空格

python - BS HTML 解析 - & 在打印 URL 字符串时被忽略

python - 在Python中将文件的格式化内容保存到另一个文件

python - 在条件匹配时使用 np.where 或类似方法创建数据框列

python - ODR错误: fcn does not output [n]-shaped array

python - 查看页面源代码时发现抓取代码

python - 尝试从网页监控产品,但未按预期工作

python - python 中的 RESTful API 请求 [tensorflow 服务]