Pages : 1
#1 Le 19/04/2018, à 07:20
- karamelo
[RESOLU] Python parsing table
Bonjour, je suis confronté à un petit soucis
Via python je cherche à récupérer dans une table html un statut qui est variable, je cherche à récupérer la valeur réparation avec le numéro correspondant .
La table a cette structure là:
<tr>
<td>103pl</td>
<td>
<span class="label success">Libre</span>
</td>
<td class="text-center"> <span class="label ">
ok
</span>
</td>
<td class="pull-right">
<a class="button button-small" href="/stat/id/215/repair/start">Départ</a>
<form action="/stat/id/215" method="post" style="display: inline;">
<input type="hidden" name="_method" value="DELETE">
<button class="button secondary alert confirm no-margin" data-open="deleteModal" type="submit" title="Supprimer">
<span><i class="fa fa-trash"></i> Supprimer</span>
</button>
</form>
</td>
</tr>
<tr>
<td>072jo</td>
<td>
<span class="label success">Libre</span>
</td>
<td class="text-center"> <span class="label ">
ok
</span>
</td>
<td class="pull-right">
<a class="button button-small" href="/stat/id/214/repair/start">Départ</a>
<form action="/stat/id/214" method="post" style="display: inline;">
<input type="hidden" name="_method" value="DELETE">
<button class="button secondary alert confirm no-margin" data-open="deleteModal" type="submit" title="Supprimer">
<span><i class="fa fa-trash"></i> Supprimer</span>
</button>
</form>
</td>
</tr>
<tr>
<td>039cas</td>
<td>
<span class="label success">Libre</span>
</td>
<td class="text-center"> <span class="label warning">
réparation
</span>
</td>
<td class="pull-right">
<a class="button button-small" href="/stat/id/211/repair/show">Voir</a>
<form action="/stat/id/211" method="post" style="display: inline;">
<input type="hidden" name="_method" value="DELETE">
<button class="button secondary alert confirm no-margin" data-open="deleteModal" type="submit" title="Supprimer">
<span><i class="fa fa-trash"></i> Supprimer</span>
</button>
</form>
</td>
</tr>
<tr>
<td>043cas</td>
<td>
<span class="label success">Libre</span>
</td>
<td class="text-center"> <span class="label warning">
réparation
</span>
</td>
<td class="pull-right">
<a class="button button-small" href="/stat/id/211/repair/show">Voir</a>
<form action="/stat/id/211" method="post" style="display: inline;">
<input type="hidden" name="_method" value="DELETE">
<button class="button secondary alert confirm no-margin" data-open="deleteModal" type="submit" title="Supprimer">
<span><i class="fa fa-trash"></i> Supprimer</span>
</button>
</form>
</td>
</tr>
Je souhaite comme sortie le(s) numéro(s) avec la valeur "réparation":
039cas, 043cas
Je tourne autour de la class "label warning" sans grand succès, après multiples essais avec BeautifulSoup je n'arrive à rien je pense ne pas être sur le bon chemin.
Un avis éclairé serait le bienvenue
merci
Dernière modification par karamelo (Le 20/04/2018, à 10:31)
Hors ligne
#2 Le 20/04/2018, à 08:05
- grim7reaper
Re : [RESOLU] Python parsing table
Salut karamelo,
C’est le genre de truc qui peut se faire très facilement en utilisant XPath.
En Python, tu peux utiliser le module lxml.
Pour ton problème, je vais considérer que ton HTML est contenu dans une chaîne de caractère nommé html.
>>> import lxml.html
>>> root = lxml.html.fromstring(html)
>>> root.xpath('//tr[td/span[@class="label warning"]]/td[1]/text()')
['039cas', '043cas']
Si tu n’es pas familier, je vais essayer d’expliquer la requête XPath :
- // => tu cherches n’importe où dans le document (en gros c’est un chemin relatif un peu comme "Documents" vs "/home/user/Document")
- //tr => on cherche toutes les balises tr du documents
- //tr[…] => les [] permettent d'ajouter des conditions pour filtrer les résultats
- //tr[td/span[@class="label warning"]] => on cherche toutes les balises tr qui contiennent une balise td qui contient une balise span avec la classe "label warning"
- //tr[td/span[@class="label warning"]]/td[1] => on récupère le premier td des tr qui match la condition expliqué ci-dessus.
- //tr[td/span[@class="label warning"]]/td[1]/text() => on récupère le contenu du premier td des tr qui match la condition précedemment.
Voilà, j’espère que c’est un peu plus clair, au pire il y a pas mal de ressources qui expliquent XPath (ici et là par exemple).
Hors ligne
#3 Le 20/04/2018, à 10:30
- karamelo
Re : [RESOLU] Python parsing table
Arrrfff mais quel boulet je fais
j'avais trouvé une solution cracra pour palier mon manque de RTFM visiblement .... python GREP AWK SED
mais là c'est vraiment plus simple et bien expliqué de surcroît
merci pour ton aide
bonne journée
Dernière modification par karamelo (Le 20/04/2018, à 10:31)
Hors ligne
Pages : 1