Beautiful Soup中的部分类名搜寻

2024-09-30 05:17:09 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

我试图自动提取价格(在这个例子中是800美元)。但是,随着时间的推移,“currentPrice-”后面的数字会发生变化,我的Python脚本将停止工作。我正在使用这个漂亮的汤功能:

^{pr2}$

如何使用find_all来提取类名的部分匹配,例如包含字符串“currentPrice-”的所有类?在


Tags: httpswwwdecaspanmontrealvillebedroom
2条回答

根据docs你有几个选择:

  • 使用正则表达式:

    soup.find_all('span', attrs={'class': re.compile('^currentPrice.*')})
    
  • 使用函数:

    soup.find_all('span',
                  attrs={'class': lambda e: e.startswith('currentPrice') if e else False})
    

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

相关问题 更多 >

    热门问题