Bonjour..j'essaye..
1°) Comment faire une conversion
Pour simplifier, En Python 3000, de façon interne, le format texte (str) prend en compte tout les caractères sous un format spécial.
Si on fourni une suite d'octet (bytes) il faut indiquer à l'interpréteur l'encodage (le langage) pour que python le comprenne et le convertisse en texte.
d'après
http://docs.python.org/dev/3.0/whatsnew/3.0.html
Use str.encode() to go from str to bytes, and bytes.decode() to go from bytes to str
2°) Paramètre de conversion
D'après l'analyse de la page,il s'agit d'un encodage "ISO-8859-1.
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
3°) La commande complète
Je demande donc un décodage pour passer de bytes (au format "ISO-8859-1") vers str
h=h.decode("ISO-8859-1")
4°) Puis je fait une visu du résultat
:
1 2 3 4 5 6 7 8 9 10 11 12
| import urllib.request, urllib.parse, urllib.error
requete = urllib.request.Request('http://www.google.fr')
resultat = urllib.request.urlopen(requete)
h=resultat.read()
h=h.decode("ISO-8859-1")
print(type(h))
print ("------------------------------------------------")
for c in h: # Visu
print (c,end='')
if c==">" :
print(end='\n') |
Partager