Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

python - Find partial class names in spans with Beautiful Soup

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-"?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...