Python 将正则表达式作为参数传递的BeautifulSoup
我有这个html:Python 将正则表达式作为参数传递的BeautifulSoup,python,regex,python-2.7,beautifulsoup,Python,Regex,Python 2.7,Beautifulsoup,我有这个html: title="Keeper: Michal Buchalik" class="pos_text">Buchalik</a></span> <span class="pos_text pos3_l_5"> 它与任何内容都不匹配,并且正则表达式肯定有问题,因为当我键入某个数字来代替\d{1,2}时,它工作正常 由于是p
title="Keeper: Michal Buchalik" class="pos_text">Buchalik</a></span>
<span class="pos_text pos3_l_5">
它与任何内容都不匹配,并且正则表达式肯定有问题,因为当我键入某个数字来代替
\d{1,2}
时,它工作正常 由于是python,您需要使用r表示“原始文本”或转义“\”字符:
re.compile(r"pos_text pos3_l_\d{1,2}")
OR
re.compile("pos_text pos3_l_\\d{1,2}")
看看是否有帮助
干杯。谢谢您的回复,但在上述情况下不需要逃跑。请注意,在原始HTML页面中,没有反斜杠字符。是的,但您的正则表达式使用\d(数字)结构。因此,需要转义的不是文本,而是正则表达式本身。试一试?似乎问题不在正则表达式上-因为re.compile(“pos_text pos3\u l\d{1,2}”).split()似乎按预期运行。我在正则表达式中发现了一个明显的错误,这部分代码缺少
类=
,尽管它仍然没有给我预期的输出。
re.compile(r"pos_text pos3_l_\d{1,2}")
OR
re.compile("pos_text pos3_l_\\d{1,2}")