本文介绍了使用Beautiful Soup在跨度中查找部分类名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此页面 https://www.kijiji.ca/v-1-bedroom-apartments-condos/ville-de-montreal/1-chambre-chauff-eau-chaude -incl-vsl-514-856-0038/1334431659 包含以下跨度类:

This page https://www.kijiji.ca/v-1-bedroom-apartments-condos/ville-de-montreal/1-chambre-chauff-eau-chaude-incl-vsl-514-856-0038/1334431659 contains this span class:

<span class="currentPrice-3131760660"><span content="800.00">800,00 $</span>

我正在尝试自动提取价格(在这种情况下为800 $).但是,随着时间的流逝,"currentPrice-"之后的数字发生了变化,并且我的Python脚本停止工作.我正在使用此美丽汤功能:

I'm trying to automatically extract the price (800$ in this case). Over time, however, the number after "currentPrice-" changes, and my Python script ceases to work. I am using this Beautiful soup function:

soup.find_all('span', {'class' : 'currentPrice-3131760660'})

如何使用find_all提取类名称的部分匹配项,例如包含字符串"currentPrice-"的所有类?

How can I use find_all to extract partial matches of class names, such as all classes containing the string "currentPrice-"?

推荐答案

您可以尝试CSS选择器soup.select('span[class*="currentPrice-"]')

You could try a CSS selector soup.select('span[class*="currentPrice-"]')

这篇关于使用Beautiful Soup在跨度中查找部分类名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 20:09