Guest User

Untitled

a guest
Apr 23rd, 2020
533
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # coding: utf-8
  2. # adapted from: https://freres.peyronnet.eu/scraping-en-python-pour-etre-alerte-des-nouveaux-creneaux-drive-chez-super-u/
  3.  
  4. import requests
  5. from bs4 import BeautifulSoup
  6. import time
  7. from lxml import html
  8. from tkinter import Label, Entry, Button, Tk
  9. import tkinter as tk
  10.  
  11.  
  12. def main_search_loop(slot_string_var, url_entry, refresh):
  13.     slot_string_var.set("Créneau disponible: (recherche en cours...)")
  14.     while True:
  15.         resp = requests.get(url_entry.get())
  16.         data = resp.content
  17.         soup = BeautifulSoup(data, "lxml")
  18.         tree = html.fromstring(data)
  19.         isit = tree.xpath('//a[@class="store__content-delivery hidden"]/text()')
  20.         if not len(isit) or isit[0] == "Information momentanément indisponible":
  21.             slot_string_var.set(
  22.                 'Créneau disponible: Information momentanément indisponibles, nouvelle tentative dans {} minute(s)'.format(refresh.get())
  23.             )
  24.             time.sleep(int(refresh.get())*60)
  25.             continue
  26.         else:
  27.             slot_string_var.set('Créneau disponible: {}'.format(isit[0]))
  28.             print('créneau disponible: {}'.format(isit[0]))
  29.             break
  30.  
  31.  
  32. window = Tk()
  33.  
  34. window.title("drive-u delivery slot tracker")
  35. window.geometry('650x400')
  36.  
  37. url_label = Label(window, text="URL du magasin:")
  38. url_label.grid(column=0, row=0)
  39.  
  40. url_entry = Entry(window, width=50)
  41. url_entry.insert(0, "https://www.coursesu.com/drive-superu-montpellier")
  42. url_entry.grid(column=0, row=1)
  43.  
  44.  
  45. refresh_label = Label(window, text="Rafraichir toutes les (minutes): ")
  46. refresh_label.grid(column=0, row=2)
  47.  
  48. refresh_entry = Entry(window, width=4, text='1')
  49. refresh_entry.insert(0, '1')
  50. refresh_entry.grid(column=1, row=2)
  51.  
  52. slot_string_var = tk.StringVar(value="Créneau disponible: ")
  53. slot_label = Label(window, textvariable=slot_string_var)
  54. slot_label.grid(column=0, row=3)
  55. btn = Button(window, text="Rechercher", command= lambda: main_search_loop(slot_string_var,
  56.                                                                           url_entry,
  57.                                                                           refresh_entry))
  58. btn.grid(column=4, row=4)
  59. window.mainloop()
RAW Paste Data