Sid
Sid

Reputation: 4055

BeautifulSoup - how to get the second next item in beautiful soup?

<div _ngcontent-c2=""><span _ngcontent-c2="" class="text-secondary">Claim date: </span><span _ngcontent-c2="" class="text-primary">26 September, 2018</span></div>

I want to find the 26 September, 2018 text from above after finding the Claim date:.

I have tried findNext, find_next_sibling and a lot of other suggestions.

There are multiple _ngcontent2 and text-primary.

Thanks for your help.

Upvotes: 1

Views: 637

Answers (1)

KunduK
KunduK

Reputation: 33384

You can use regex.Import re. and then find the text of the span tag and then find_next span tag.

import re
html='''<div _ngcontent-c2=""><span _ngcontent-c2="" class="text-secondary">Claim date: </span><span _ngcontent-c2="" class="text-primary">26 September, 2018</span></div>'''
soup=BeautifulSoup(html,'html.parser')
print(soup.find('span',text=re.compile('Claim date:')).find_next('span').text)

Output:

26 September, 2018

OR if you have bs4 4.7.1 or above you can use css selector.

html='''<div _ngcontent-c2=""><span _ngcontent-c2="" class="text-secondary">Claim date: </span><span _ngcontent-c2="" class="text-primary">26 September, 2018</span></div>'''
soup=BeautifulSoup(html,'html.parser')
print(soup.select_one('span:contains("Claim date:")').find_next('span').text)

Upvotes: 2

Related Questions