3

我有一个 BeautifulSoup 的“问题”,尤其是 re 模块这是问题所在:

import re

from bs4 import BeautifulSoup

string = """
<div id="my_id">
    <ul>
        <li>something</li>
        <li class="color12">something</li>
        <li class="color45">something else</li>
    </ul>
</div>
"""
soup = BeautifulSoup(string)
li = soup.find_all('li', {'class': re.compile('color(\d+)')} )
for ele in li:
    print ele['class'] # will print colorXXXX but i would like to know how to get only this XXXX

但我只想提取颜色后的数字。是否有可能或者我有义务使用类似的东西:

match = re.search(r'color(\d+)', str(ele['class']))
if match:
    print match.group(1)

谢谢你的帮忙 :)

4

1 回答 1

3

您必须重新应用正则表达式。只需将其存储在变量中并重用:

colorpattern = re.compile(r'color(\d+)')

li = soup.find_all('li', {'class': colorpattern} )
for ele in li:
    print colorpattern.search(ele['class']).group(1)
于 2012-11-15T15:34:29.400 回答