Solution 1 :

Use strip() before you split lines because it may have empty lines at start.

And I had to use n instead of rn

for br in soup.findAll('p'):
    text = br.text.strip().split('n')[0].replace('?','')
    print(text)

Eventually you can use get_text(strip=True) but it needs separator="n" to keep n inside text

for br in soup.findAll('p'):
    text = br.get_text(strip=True, separator='n').split('n')[0].replace('?','')
    print(text)

Solution 2 :

You can try:

for br in soup.select('p > br:nth-of-type(1)'):
    print(br.previous_sibling)

Output

 A di gàárì sílẹ̀ ewúrẹ́ ńyọjú; ẹrù ìran rẹ̀ ni?

 A fi ọ́ jọba ò ńṣàwúre o fẹ́ jẹ Ọlọ́run ni?

 A fijó gba Awà; a fìjà gba Awà; bí a ò bá jó, bí a ò bá jà, bí a bá ti gba Awà, kò tán bí?

Solution 3 :

Use contents[0] which retrieve the first text value of the P tag.

html = """
[<p xmlns_ino="http://namespaces.softwareag.com/tamino/response2" xmlns_xq="http://namespaces.softwareag.com/tamino/XQuery/result" xmlns_xql="http://metalab.unc.edu/xql/">
 A di gàárì sílẹ̀ ewúrẹ́ ńyọjú; ẹrù ìran rẹ̀ ni?<br/>
 We prepare the saddle, and the goat presents itself; is it a burden for the lineage of goats?<br/>
 (Goats that know their place do not offer their backs to be saddled.)<br/>
 This is a variant of A gbé gàárì ọmọ ewúrẹ́ ńrojú . . .<br/>
 </p>,
 <p xmlns_ino="http://namespaces.softwareag.com/tamino/response2" xmlns_xq="http://namespaces.softwareag.com/tamino/XQuery/result" xmlns_xql="http://metalab.unc.edu/xql/">
 A fi ọ́ jọba ò ńṣàwúre o fẹ́ jẹ Ọlọ́run ni?<br/>
 You have been crowned a king, and yet you make good-luck charms; would you be crowned God?<br/>
 (Being crowned a king is about the best fortune a mortal could hope for.)<br/>
 </p>,
 <p xmlns_ino="http://namespaces.softwareag.com/tamino/response2" xmlns_xq="http://namespaces.softwareag.com/tamino/XQuery/result" xmlns_xql="http://metalab.unc.edu/xql/">
 A fijó gba Awà; a fìjà gba Awà; bí a ò bá jó, bí a ò bá jà, bí a bá ti gba Awà, kò tán bí?<br/> 
 By dancing we take possession of Awà; through fighting we take possession of Awà; if we neither dance nor fight, but take possession of Awà anyway, is the result not the same?<br/>
 (Why make a huge production of a matter that is easily taken care of?)<br/>
 </p>]
"""
soup=BeautifulSoup(html,'html.parser')
for ptag in soup.find_all('p'):
    print(ptag.contents[0])

Output:

A di gàárì sílẹ̀ ewúrẹ́ ńyọjú; ẹrù ìran rẹ̀ ni?

 A fi ọ́ jọba ò ńṣàwúre o fẹ́ jẹ Ọlọ́run ni?

 A fijó gba Awà; a fìjà gba Awà; bí a ò bá jó, bí a ò bá jà, bí a bá ti gba Awà, kò tán bí?

Problem :

i tried extracting some text in an html tag but i couldn’t get it. i only want to extract

A di gàárì sílẹ̀ ewúrẹ́ ńyọjú; ẹrù ìran rẹ̀ ni
A fi ọ́ jọba ò ńṣàwúre o fẹ́ jẹ Ọlọ́run ni,
A fijó gba Awà;a fìjà gba Awà; bí a ò bá jó, bí a ò bá jà, bí a bá ti gba Awà, kò tán bí?

then add them to a list

from bs4 import BeautifulSoup
soup =BeautifulSoup(html, 'html.parser')

per = {'data':[]}

for br in soup.findAll('p'):
        text = br.text#.split('rn')[0].replace('?','')
html = """
[<p xmlns_ino="http://namespaces.softwareag.com/tamino/response2" xmlns_xq="http://namespaces.softwareag.com/tamino/XQuery/result" xmlns_xql="http://metalab.unc.edu/xql/">
 A di gàárì sílẹ̀ ewúrẹ́ ńyọjú; ẹrù ìran rẹ̀ ni?<br/>
 We prepare the saddle, and the goat presents itself; is it a burden for the lineage of goats?<br/>
 (Goats that know their place do not offer their backs to be saddled.)<br/>
 This is a variant of A gbé gàárì ọmọ ewúrẹ́ ńrojú . . .<br/>
 </p>,
 <p xmlns_ino="http://namespaces.softwareag.com/tamino/response2" xmlns_xq="http://namespaces.softwareag.com/tamino/XQuery/result" xmlns_xql="http://metalab.unc.edu/xql/">
 A fi ọ́ jọba ò ńṣàwúre o fẹ́ jẹ Ọlọ́run ni?<br/>
 You have been crowned a king, and yet you make good-luck charms; would you be crowned God?<br/>
 (Being crowned a king is about the best fortune a mortal could hope for.)<br/>
 </p>,
 <p xmlns_ino="http://namespaces.softwareag.com/tamino/response2" xmlns_xq="http://namespaces.softwareag.com/tamino/XQuery/result" xmlns_xql="http://metalab.unc.edu/xql/">
 A fijó gba Awà; a fìjà gba Awà; bí a ò bá jó, bí a ò bá jà, bí a bá ti gba Awà, kò tán bí?<br/> 
 By dancing we take possession of Awà; through fighting we take possession of Awà; if we neither dance nor fight, but take possession of Awà anyway, is the result not the same?<br/>
 (Why make a huge production of a matter that is easily taken care of?)<br/>
 </p>]
"""

By

Leave a Reply

Your email address will not be published. Required fields are marked *