Извлечение содержимого из тега html в python - PullRequest
0 голосов
/ 24 января 2020

Я пытался извлечь некоторый текст в теге html, но не смог его получить. я только хочу извлечь

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

и добавить их в список

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

per = {'data':[]}

for br in soup.findAll('p'):
        text = br.text#.split('\r\n')[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>]
"""

Ответы [ 3 ]

0 голосов
/ 24 января 2020

Вы можете попробовать:

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

Выход

 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í?
0 голосов
/ 24 января 2020

Используйте contents[0], который retrieve первое текстовое значение 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])

Выход :

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í?
0 голосов
/ 24 января 2020

Используйте strip() перед разделением строк, потому что в начале могут быть пустые строки.

И мне пришлось использовать \n вместо \r\n

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

В конце концов, вы можете использовать get_text(strip=True), но ему нужно separator="\n", чтобы держать \n внутри текст

for br in soup.findAll('p'):
    text = br.get_text(strip=True, separator='\n').split('\n')[0].replace('?','')
    print(text)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...