0

我有这个html:

title="Keeper: Michal Buchalik" class="pos_text">Buchalik</a></span>                
                                            <span class="pos_text pos3_l_5">

我尝试匹配Buchalik

我想出了这段代码:

for gk in soup.find_all(re.compile("pos_text pos3_l_\d{1,2}")):
    print gk.previous_element.previous_element,

它不匹配任何东西,并且正则表达式一定有问题,因为当我输入某个数字代替它时,\d{1,2}它工作得很好。

4

1 回答 1

1

由于它是 python,因此您需要将 r 用于“原始文本”或转义 '\' 字符:

re.compile(r"pos_text pos3_l_\d{1,2}")

OR

re.compile("pos_text pos3_l_\\d{1,2}")

看看有没有帮助。

干杯。

于 2013-04-06T13:40:41.163 回答