BeautifulSoup: how to find all the about attributes from html string

Viewed 315

In a text file, these items have the same structure and I would like to parse it with beautiful soup.

An extract:

data = """<text id="1" sig="prenatfra-camppres-2017-part01-viewEvent-1&docRefId-0&docName-news%C2%B720170425%C2%B7LC%C2%B7assignment_862852&docIndex-3_1" title="Éditorial élection présidentielle" author="NULL" year="2017" date="25/04/2017" section="NULL" sourcename="La Croix" sourcesig="LC" polarity="Positif" about="Le Pen|Macron">
<p type="title">Éditorial élection présidentielle</p>
</text>"""

soup = BeautifulSoup(data, 'html.parser') # 

I know data are not really a pure html code. I would like to extract all "about" section for example.

print(soup.find_all('about')) => it returns an empty array!

Perhaps I use a wrong parser? Thanks a lot. Best regards. Théo

1 Answers

If you check the documentation carefully for find_all, it looks for tags with the specified name.

So in this case, you should look for the text tag(s) and then retrieve the about attribute from them.

A working example would look like this:

from bs4 import BeautifulSoup

data = """<text id="1" sig="prenatfra-camppres-2017-part01-viewEvent-1&docRefId-0&docName-news%C2%B720170425%C2%B7LC%C2%B7assignment_862852&docIndex-3_1" title="Éditorial élection présidentielle" author="NULL" year="2017" date="25/04/2017" section="NULL" sourcename="La Croix" sourcesig="LC" polarity="Positif" about="Le Pen|Macron">
<p type="title">Éditorial élection présidentielle</p>
</text>"""

soup = BeautifulSoup(data, 'html.parser')

# to get the 'about' attribute from the first text element
print(soup.find_all('text')[0]['about'])

# to get the 'about' attributes from all the text elements, as a list
print([text['about'] for text in soup.find_all('text')])

Output:

Le Pen|Macron
['Le Pen|Macron']
Related