SHARE
TWEET

Tkinter - Pic Downloader

furas Jul 24th, 2018 (edited) 23 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import os
  2. import tkinter as tk
  3. import requests
  4. import bs4
  5. import urllib.parse
  6.  
  7. # --- constanst --- (UPPER_CASE names)
  8.  
  9. FOLDER = "pictures"
  10. #FOLDER = "Obrazy"
  11.  
  12. # --- functions ---
  13.  
  14. def download(): # readable name
  15.     link = ent.get()
  16.     info = requests.get(link)
  17.     code = bs4.BeautifulSoup(info.text, "lxml")#08033007723
  18.     images = code.select("img")
  19.  
  20.     for image in images:
  21.         src = image.get("src")
  22.  
  23.         #name = src[pos:]
  24.         name = src.split('/')[-1]
  25.  
  26.         info_label['text'] = name
  27.         root.update() # to force tkinter to update widgets
  28.  
  29.         url = urllib.parse.urljoin(link, src)
  30.  
  31.         infos = requests.get(url)
  32.         files = open(os.path.join(FOLDER, name), 'wb')
  33.         files.write(infos.content)
  34.         files.close()
  35.        
  36.     info_label['text'] = '< finished >'
  37.  
  38. # --- main ---
  39.  
  40. root = tk.Tk()
  41. root.title("Pics Downloader")
  42. root.geometry("400x200")
  43.  
  44. label = tk.Label(root, text="Link to page with image(s):") # no spaces around =
  45. label.pack(fill='x', padx=5, pady=5)
  46.  
  47. ent = tk.Entry(root)
  48. ent.pack(fill='x', padx=5, pady=(0,5))
  49.  
  50. but = tk.Button(root, text="Download", command=download)
  51. but.pack(fill='x', padx=5, pady=(0,5))
  52.  
  53. info_label = tk.Label(root, text="") # label to display downloaded images
  54. info_label.pack(fill='x', padx=5, pady=5)
  55.  
  56. root.mainloop()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top