我想将两个正则表达式组合成一行。
soup1=link.findAll('a', attrs={'href': re.compile('^http://')})
soup2=link.findAll('a', attrs={'href': re.compile("/news/")})
我尝试了 (|)
登录 re.compile('^http://' | '/news/')
但一切都是徒劳。我需要这两个功能(包含 'http'
以及 /news/
的链接)
最佳答案
您不需要正则表达式,您可以使用css选择器:
soup.select('a[href^=http://],a[href*=/news/]')
^=
查找以子字符串开头的 href,*=
查找在任意位置包含子字符串的 href。
关于regex - 如何在python3中组合两个re.compile正则表达式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38322393/