Python查找所有正则表达式字符串值

Python查找所有正则表达式字符串值,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 及 但这两个代码只返回与“news

我想找到所有链接标题(使用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


但这两个代码只返回与“news”匹配的结果,而不返回与“reporting”匹配的结果,所以不知道如何实现这一点?提前谢谢

你应该朝着这样的方向看

search = re.compile(r"reporting|news")
for text in box.find_all("p",text=search):
   #dosth

请注意
|
字符,它在正则表达式中充当
|
可用于
任何任意正则表达式和表达式组。查看以了解更多信息。

您能分享一些您正在使用的数据吗?它将帮助我们为您解决这个问题。
search = re.compile(r"reporting|news")
for text in box.find_all("p",text=search):
   #dosth