python - Python查找所有正则表达式字符串值
问题描述
我想查找所有链接标题(使用bs4进行抓取)包含字符串:“news”或“reporting”(标题包含两个单词应该是结果)
我试过:
search = re.compile(r"news")
search1 = re.compile(r"reporting")
for text in box.find_all("p",text= search or search1):
#dosth
和
search = re.compile("news.+reporting")
for text in box.find_all("p",text= search or search1):
#dosth
但这两个代码只返回匹配“新闻”而不匹配“报道”,所以想知道如何做到这一点?提前致谢!
解决方案
你应该看看这样的东西
search = re.compile(r"reporting|news")
for text in box.find_all("p",text=search):
#dosth
注意这个|
字符,它or
在正则表达式中起作用。可|
用于or
任意正则表达式和表达式组。查看文档以获取更多信息。
推荐阅读
- mongodb - 嵌套字段 2 级别上的 Mongodb 聚合
- powershell - 获取 WinEvent 脚本
- .net - Wix BootStrapper exe无法在客户端机器上运行
- python - 无法连接到远程 Jupyter 笔记本
- powershell - 如何将程序集加载到 Powershell 的全局范围内?
- windows - Windows 性能记录器收集堆快照时出错。错误代码:0x80070032
- javascript - 如何使用数据加载器?
- docker - npm 库无法在 GitLab CI 中安装
- c# - 在 C# 中存储一些不可变和高度访问的数据的最佳方法是什么?
- python-3.x - 我无法从 PythonStdioGames 启动任何程序并出现错误,我该如何防止这种情况发生?