2017-01-05 5 views
1

Ich brauche ix:nonfraction den Namen und den Wert und den Kontext ref für alle Felder unter dem Tag zu erhalten, die wie folgt aussieht:regex findall in beautifulsoup -python 3

<ix:nonfraction name="uk-gaap:TangibleFixedAssets" contextref="FY1.END" unitref="GBP" xmlns:uk-gaap="http://www.xbrl.org/uk/gaap/core/2009-09-01" decimals="0" format="ixt:numcommadot">238,011</ix:nonfraction>.

mit dem Ausgang benötigt wie:

TangibleFixedAssets, FY1.end, 238,011 

der String, der regex durch viele suchen haben diese Tags enthält, so gäbe es eine Möglichkeit geben, zu halten alle 3 Ausgänge verketteten (oder innerhalb der gleichen Index der Liste)? .

+0

Try this ** \ ([\ w \,] +) \ <\/ix: nichtfraktion \> ** . Verwenden Sie ** g ** globale Modifier –

Antwort

1
import bs4 
html = '''<ix:nonfraction name="uk-gaap:TangibleFixedAssets" contextref="FY1.END" unitref="GBP" xmlns:uk-gaap="http://www.xbrl.org/uk/gaap/core/2009-09-01" decimals="0" format="ixt:numcommadot">238,011</ix:nonfraction>''' 

soup = bs4.BeautifulSoup(html, 'lxml') 

ixs = soup.find_all('ix:nonfraction') 
for ix in ixs: 
    name = ix['name'].split(':')[-1] 
    contextref = ix['contextref'] 
    text = ix.text 
    output = [name, contextref, text] 
    print(output) 

aus:

['TangibleFixedAssets', 'FY1.END', '238,011'] 
+1

Legende. Danke vielmals – reuben