python - 使用 Beautiful Soup 在跨度中查找部分类名
问题描述
<span class="currentPrice-3131760660"><span content="800.00">800,00 $</span>
我正在尝试自动提取价格(在这种情况下为 800 美元)。然而,随着时间的推移,“currentPrice-”之后的数字会发生变化,我的 Python 脚本将停止工作。我正在使用这个美丽的汤功能:
soup.find_all('span', {'class' : 'currentPrice-3131760660'})
如何使用 find_all 提取类名的部分匹配项,例如包含字符串“currentPrice-”的所有类?
解决方案
根据文档,您有几个选择:
使用正则表达式:
soup.find_all('span', attrs={'class': re.compile('^currentPrice.*')})
使用一个函数:
soup.find_all('span', attrs={'class': lambda e: e.startswith('currentPrice') if e else False})
推荐阅读
- c# - 光子螺栓 | 创建后将房间设为私有
- rest - 跨多个组织的基于网络的长期应用程序在 REST 中意味着什么?
- python-3.x - 通过映射 df2 中的同一列并在 pandas 中应用特定计算,在 df1 中创建新列
- sql - 在sql中用NULL替换列中的字符串
- flutter - 如何防止广告在 Flutter_native_admob 中的 ListView/GridView 上重新加载?
- r - 如何为 R 中的新列选择三个值之一?
- swiftui - 将数组内对象的属性作为绑定传递
- javascript - 角度打字稿页面上的数组为空
- nim-lang - 如何在 Nim 中不跳行打印
- c - 如何用c编写一个可以改变服务器上视频/音频进度的服务器?