Traducteur de page html avec BeautifulSoup

version Python 3

Comme avec HTMLParser, il suffit de surcharger les méthodes handle_data et handle_starttag.

Commençons par une page basique, sans images.

In [1]:
from bs4 import BeautifulSoup as bs
In [2]:
from urllib.request import *
In [3]:
u = 'http://igm.univ-mlv.fr/~jyt/M1_python'
In [4]:
s = urlopen(u).read()
In [5]:
s
Out[5]:
b'<html>\n\t<body>\n\t\t<H1> M1 Informatique : Python </H1>\n\t\t<ul>\n\t\t\t<li> Commencer ici : <a href="http://www.python.org/">python.org</a>\n\t\t\t<li> La <a href="http://docs.python.org/3.7">documentation</a> officielle\n\t\t\t<li> Le <a href="http://www.pythonchallenge.com/"> Python Challenge</a>\n\t\t\t<li> <a href="https://python.developpez.com/cours/DiveIntoPython/"><i>Dive into Python</i></a>  \n\t\t\tpar <a href="http://en.wikipedia.org/wiki/Mark_Pilgrim_%28software_developer%29">Mark Pilgrim</a> (un livre recommand&eacute; mais ancien);\n\t\t\t<a href="http://diveintopython3.problemsolving.io/">version Python 3</a>. \n\t\t\t<li> La <a href="http://fr.wikipedia.org/wiki/Python_%28langage%29">page</a> de Wikip&eacute;dia contient un bon r&eacute;sum&eacute;\n\t\t\t<li> Introduction au <i>notebook</i> ipython ou jupyter : <a href="intro_jupyter.html">html</a>, <a href="intro_jupyter.ipynb">ipynb</a>\n\t\t\t<li>Cours 1 (26/09/2019)\n\t\t\t<ul> \n\t\t\t\t<li> Python 3 <a href="python3_M1_2019-1.html">html</a>, <a href="python3_M1_2019-1.ipynb">ipynb</a> \n\t\t\t\t<li> Python 2 <a href="python_M1_2017-1.html">html</a>, <a href="python_M1_2017-1.ipynb">ipynb</a>\n\t\t\t</ul>\n\t\t\t<a href="td1_python3_2019.html"> TD 1</a> (python 3) (26/09/2019) <a href="td1.html">Ancienne version</a> (python 2) \n\t\t\t<li>Mod&egrave;le de script : <a href="script.py.html">python 2</a> <a href="script3.py.html">python 3</a>\n\t\t\t<li> Corrig&eacute; du TD 1\n\t\t\t<ul>\n\t\t\t\t<li><a href="td1_sol_3.html">Python 3</a>\n\t\t\t\t<li><a href="td1_sol.html">Python 2</a> (ancienne version)\n\t\t\t</ul>\n\t\t\t<li> Cours 2 (3/10/2019) :\n\t\t\t<ul>\n\t\t\t\t<li> Python 3  <a href="Python3_M1_2019-2.html">html</a>, <a href="Python3_M1_2019-2.ipynb">ipynb</a>\n\t\t\t\t<li> Python 2 <a href="python_M1_2017-2.html">html</a>, <a href="python_M1_2017-2.ipynb">ipynb</a>\n\t\t\t</ul>\n\t\t\t<li><a href="td2.html">TD 2</a> (3/10/2019) \n\t\t\t<li> Corrig&eacute; du TD 2\n\t\t\t<ul>\n\t\t\t\t<li><a href="td2_sol.html">Python 2</a>\n\t\t\t\t<li><a href="td2_solpy3.html">Python 3</a>\n\t\t\t</ul>\n\t\t\t<li> Cours 3 (10/10/2019) :\n\t\t\t<ul> \n\t\t\t\t<li> Python 2 <a href="python_M1_2017-3.html">html</a>, <a href="python_M1_2017-3.ipynb">ipynb</a>\n\t\t\t\t<li> Python 3 <a href="python3_M1_2018-3.html">html</a>, <a href="python3_M1_2018-3.ipynb">ipynb</a> \n\t\t\t</ul>\n\t\t\t<li> <a href="td3_2017_texte.html">TD 3</a> (10/10/2019)\n\t\t\t<li> Corrig&eacute; du TD 3\n\t\t\t<ul>\n\t\t\t\t<li><a href="td3-sol.html">Python 2</a>\n\t\t\t\t<li><a href="soltd3_py3_part.html">Python 3</a>\n\n\t\t\t<li> Cours 4 (17/10/2019)\n\t\t\t<ul>\n\t\t\t\t<li> Python 3 <a href="Python3_M1_2019_4.html">html</a>, <a href="Python3_M1_2019_4.ipynb">ipynb</a>\n\t\t\t\t<li> Python 2 <a href="python_M1_2017-4.html">html</a>, <a href="python_M1_2017-4.ipynb">ipynb</a>\n\t\t\t\t<li> Brouilleur de pages web avec BeautifulSoup (Python 2) <a href="bs_blurr.html">html</a>, <a href="bs_blurr.ipynb">ipynb</a>\n\t\t\t</ul>\n\n\n\n\t\t\t\n\t\t\n\n\n\t\t\t\n\n\t\t</ul>\n\t</body>\n</html>\n\n\t\t\n'
In [6]:
# Touillage d'un mot comme dans le TD2

import random, re
p = re.compile('(\w)(\w\w+)(\w)', re.M)
def touille(m):
    milieu = list(m.group(2))
    random.shuffle(milieu)
    return m.group(1) + ''.join(milieu) + m.group(3)
In [9]:
# Pour commencer, on ne modifie que le texte
# Pour surcharger handle_data, on créee une classe dérivée de BeautilfulSoup

class mySoup(bs):
    def __init__(self,s):
        bs.__init__(self,s)
        
    def handle_data(self,x):
        self.current_data.append(p.sub(touille,x)) # !!!
In [10]:
soup = mySoup(s)
In [31]:
print soup.getText()
 M1 Ifnrmtqoiaue : Pyhton 

 Ceomecmnr ici : ptohyn.org
 La deoatnmtoiucn oleflcifie
			 Le  Ptohyn Caleghnle
 Dvie into Pyhotn  
			par Mark Pirligm (un lrvie rmcneaomdé);
			vsioren Pthoyn 3. 
			 La pgae de Wikipédia cieontnt un bon résumé
			 Irotntdcuoin au noooebtk iptyhon ou jtupyer : hmtl, ipnyb
 Cuors 1 (29/09/2017)
				 Python 2 hmtl, iypnb
 Poythn 3 html, inypb

 TD 1 (29/09/2107)
			 Coirrgé du TD 1
			
Photyn 2
Photyn 3

Modèle de siprct
			 Cours 2 (13/10/2107) : html, iypnb
TD 2 (13/10/2017)
			

		

In [11]:
# pour visualiser, on sauvegarde dans un fichier
open('bla.html','w').write(str(soup))
Out[11]:
2816

screen4.png

In [12]:
# Pour récupérer des liens fonctionnels et les images,
# il faut maintenant surcharger handle_starttag
# On se contentera des attributs href et src, 
# mais il resterait beaucoup d'autres cas à prendre en compte pour être complet.

from urllib.parse import urljoin

class newSoup(mySoup):
    def __init__(self,s,u):
        self.url = u
        mySoup.__init__(self,s)
    
    def handle_starttag(self,name,namespace,nsprefix,attrs):
        if 'href' in attrs: attrs['href'] = urljoin(self.url,attrs['href'])
        if 'src' in attrs: attrs['src'] = urljoin(self.url,attrs['src'])
        mySoup.handle_starttag(self,name,namespace,nsprefix,attrs)
        
In [13]:
u ='http://igm.univ-mlv.fr/~jyt/'
s = urlopen(u).read()
soup=newSoup(s,u)
In [14]:
open('blu.html','w').write(str(soup))
Out[14]:
7140

screen5.g

In [ ]: