Find partial class names in spans with Beautiful Soup

梦想与她 提交于 2020-05-17 07:30:27

问题


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>

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'})

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


回答1:


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




回答2:


According to the docs you have several options:

  • Use a regex:

    soup.find_all('span', attrs={'class': re.compile('^currentPrice.*')})
    
  • Use a function:

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


来源:https://stackoverflow.com/questions/52842778/find-partial-class-names-in-spans-with-beautiful-soup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!