Select second element from HTML tag with Python/BeautifulSoup


I’m currently scraping elements from a webpage. Let’s say i’m iterating over a html reponse and a part of that response looks like this:

<div class="col-sm-12 col-md-5">
<div class="material">
<div class="material-parts">
<span class="material-part" title="SLT-4 2435">
<img src="/images/train-material/mat_slt4.png"/> </span>
<span class="material-part" title="SLT-6 2631">
<img src="/images/train-material/mat_slt6.png"/> </span>

I know I can access the first element under title within the span class like so:

"SLT-4 2435

But I would like to select the second title under the span class (if it exists) as a string too, like so: "SLT-4 2435, SLT-6 2631"

Any ideas?

>Solution :

You can use the find_all() function to find all the span elements with class material-part

titles = []
for material_part in row[-1].find_all('span', class_='material-part'):
result = ', '.join(titles)

Leave a Reply Cancel reply