#!/usr/bin/env python # -*- coding: utf-8 -*- from tkinter import * from bs4 import BeautifulSoup import urllib.request import json import sys def click (): print (value.get()) try: label2["text"]+=data[value.get()]+"\r" print(data[value.get()]) except KeyError: print ("numero de chaine inconnue") label2["text"]+="numero de chaine inconnue"+"\r" value.set("") def click_reset (): print ("exit") fenetre.quit label2["text"]="" fenetre.mainloop def load_jsonfile(file): with open(file, 'r', encoding='utf-8') as f: return json.load(f) def RepresentsInt(s): try: int(s) return True except ValueError: return False except TypeError: return False def parsechaine(): URL='https://fr.wikipedia.org/wiki/Liste_des_cha%C3%AEnes_de_Canal' liste_chaine={} response = urllib.request.urlopen(URL) html = response.read() parse=BeautifulSoup(html,"html.parser") for item in parse.find_all('table'): if (item.get("class") == ['wikitable'] or item.get("class") == ['wikitable', 'sortable'] ): for tr in item.find_all('tr'): firstTD=tr.find() num=firstTD.string if RepresentsInt(num): #print (num) if RepresentsInt(firstTD.find_next().string): #print (firstTD.find_next().find_next().string) liste_chaine[num]=firstTD.find_next().find_next().string else: #print (firstTD.find_next().string) liste_chaine[num]=firstTD.find_next().string print(json.dumps(liste_chaine, indent=4)) with open('chaine.json', 'w', encoding='utf-8') as f: json.dump(liste_chaine, f, indent=4) data=load_jsonfile('chaine.json') label2["text"]+="update chaine done"+"\r" def enter(evt): click() def eventreset(evt): click_reset() data=load_jsonfile('chaine.json') fenetre = Tk() fenetre.title("recherche de chaine") value = StringVar() if(len(sys.argv)>1): arg=True else: value.set("") arg=False label=Label(fenetre,text="entrer numero de chaine") entree = Entry(fenetre, textvariable=value, width=30) frame =Frame(fenetre) label2=Label(fenetre, text="") bouton_update_base= Button(fenetre,text="update la base de chaine",command=parsechaine) bouton=Button(frame, text="OK", command=click) reset=Button(frame, text="reset",command=click_reset) label.pack() entree.pack() entree.focus_set() frame.pack() bouton.pack(side = LEFT) reset.pack(side = RIGHT) label2.pack() bouton_update_base.pack() fenetre.bind("",enter) fenetre.bind("",eventreset) if (arg==True): for i in sys.argv[1:]: value.set(i) click() value.set("") fenetre.mainloop()