gpt4 book ai didi

python - BeautifulSoup提取物

转载 作者:行者123 更新时间:2023-11-28 16:46:26 25 4
gpt4 key购买 nike

我对 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)

谢谢你的帮助:)

最佳答案

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

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

li = soup.find_all('li', {'class': colorpattern} )
for ele in li:
print colorpattern.search(ele['class']).group(1)

关于python - BeautifulSoup提取物,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13400774/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com