-2

I am working on a script which cleans the content of posts from an old custom-build website and injects it into a new wordpress install.

Currently my script works this way: 1. read in sqlexportfile into a pandas dataframe 2. find all images used in a post, download the image and upload it to the new server, replace the url in the content 3. upload the post itself (these 3 steps all work)

However: the old cms had posts in it going back a 15 years, so I am facing tons of old (bad) habits, unexpected htmlcodes and so on.

A snippet from such a post:

<div><span style="font-size: xx-small;"><img alt="Maurits stadion" height="190" src="http://www.fortunahome.com/fotopage/specials/stadions/maurits/m2.jpg" style="margin: 5px 10px; float: left; border: black 2px solid;" width="287" /></span></div>

<p>&nbsp;</p>

<p style="text-align: justify;">De oprichter van Fortuna &#39;54 was de zakenman Egidius &#39;Gied&#39; Joosten. Op 12 maart 1953 zat Gied Joosten op de tribune in Parijs voor een wedstrijd tussen in het buitenland spelende profs en het Franse nationale elftal. De opbrengst was bestemd voor het Rampenfonds voor het door watersnood getroffen Zeeland. Het Nederlandse gelegenheidselftal won met 2-1. Joosten sloot zich aan bij de Nederlandse Beroepsvoetbalbond en werd voorzitter. Hij stimuleerde de oprichting van semi-profslubs. Huub Adriaans, de Fortuna &#39;54-manager, slaagde erin om een aansprekend elftal op de been te brengen. Bekende namen uit die tijd zijn Frans de Munck, Cor van der Hart en Faas Wilkes. In de tussentijd groeide Geleen door de staatsmijn Maurits.</p>

These posts are filled with all kinds of weird constructions which cause trouble on wordpress (eg.: the inline css parts are really messing with the mobile layout view).

The tricky part is: for the image htmltags, I want to keep those partly.

<img alt="Maurits stadion" height="190" src="http://www.fortunahome.com/fotopage/specials/stadions/maurits/m2.jpg" style="margin: 5px 10px; float: left; border: black 2px solid;" width="287" />

should become:

<img src="http://www.fortunahome.com/fotopage/specials/stadions/maurits/m2.jpg" />

now I have found the solution on Strip HTML from strings in Python

but this will strip the img-tags also completely. (note: the postdatabase I need to adjust counts about 6500 posts, so I have accepted that almost no solution will be perfect)

Janko
  • 137
  • 2
  • 14

1 Answers1

1

You can use BeautifulSoup to manipulate your tags. This code will strip all attributes but src from <img>:

from bs4 import BeautifulSoup

data = """<div><span style="font-size: xx-small;"><img alt="Maurits stadion" height="190" src="http://www.fortunahome.com/fotopage/specials/stadions/maurits/m2.jpg" style="margin: 5px 10px; float: left; border: black 2px solid;" width="287" /></span></div>

<p>&nbsp;</p>

<p style="text-align: justify;">De oprichter van Fortuna &#39;54 was de zakenman Egidius &#39;Gied&#39; Joosten. Op 12 maart 1953 zat Gied Joosten op de tribune in Parijs voor een wedstrijd tussen in het buitenland spelende profs en het Franse nationale elftal. De opbrengst was bestemd voor het Rampenfonds voor het door watersnood getroffen Zeeland. Het Nederlandse gelegenheidselftal won met 2-1. Joosten sloot zich aan bij de Nederlandse Beroepsvoetbalbond en werd voorzitter. Hij stimuleerde de oprichting van semi-profslubs. Huub Adriaans, de Fortuna &#39;54-manager, slaagde erin om een aansprekend elftal op de been te brengen. Bekende namen uit die tijd zijn Frans de Munck, Cor van der Hart en Faas Wilkes. In de tussentijd groeide Geleen door de staatsmijn Maurits.</p>"""

soup = BeautifulSoup(data, 'lxml')

for img in soup.select('img'):
    img.attrs = {'src': img['src']}

print(soup.body.prettify())

Prints:

<body>
 <div>
  <span style="font-size: xx-small;">
   <img src="http://www.fortunahome.com/fotopage/specials/stadions/maurits/m2.jpg"/>
  </span>
 </div>
 <p>
 </p>
 <p style="text-align: justify;">
  De oprichter van Fortuna '54 was de zakenman Egidius 'Gied' Joosten. Op 12 maart 1953 zat Gied Joosten op de tribune in Parijs voor een wedstrijd tussen in het buitenland spelende profs en het Franse nationale elftal. De opbrengst was bestemd voor het Rampenfonds voor het door watersnood getroffen Zeeland. Het Nederlandse gelegenheidselftal won met 2-1. Joosten sloot zich aan bij de Nederlandse Beroepsvoetbalbond en werd voorzitter. Hij stimuleerde de oprichting van semi-profslubs. Huub Adriaans, de Fortuna '54-manager, slaagde erin om een aansprekend elftal op de been te brengen. Bekende namen uit die tijd zijn Frans de Munck, Cor van der Hart en Faas Wilkes. In de tussentijd groeide Geleen door de staatsmijn Maurits.
 </p>
</body>
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
  • thank you, this helped me along the road. I used a second function now to clean up the other html-tags from which I know they cause troubles. Along with your answer, it's as close as I can get to the solution I was searching for. – Janko Jul 23 '18 at 14:37