我有一个 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)
谢谢你的帮忙 :)