Skip to content
Advertisement

Use Beautiful Soup to unify #text after a tag

I’m using Beautiful Soup to put in a excel table some infos from a website. enter image description here

The bold titles are shown in the head columns while the text after the colon appear in the rows.

What I’m doing is finding the text and searching for next_sibling –>

JavaScript

The problem is that in some cases the text after colon, is split in different #text part. So if I use the next_sibling, it’ll get only a partial info.

enter image description here

As you can see in the inspector, the content of Titoli originali: will only be “da” if I use next_sibling.

Is there a way to unify all those #text parts? How would you approach this problem? Thank you

UPDATES:

This is the website I’m scraping from –> http://www.letteraturenordiche.it/danimarca.htm

It’s giving me a hard time cause it has an incoherent structure and no use of classes.

One thing I did is to remove from the <pre> content all of the tags, <font> tags and <span> tags, to leave only the <b> ones and take the text after that.

Advertisement

Answer

Parsing this document isn’t pretty. Probable the document is hand-written in Word and then exported to HTML:

JavaScript

Creates the dataframe and saves it as data.csv (screenshot from LibreOffice):

enter image description here

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement