Advertisement
Najeebsk

IMAGES-DOWNLOAD.py

Jan 8th, 2024
742
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.89 KB | None | 0 0
  1. import os
  2. import tkinter as tk
  3. from tkinter import ttk
  4. from tkinter import filedialog
  5. import requests
  6. from bs4 import BeautifulSoup
  7. from urllib.parse import urljoin
  8.  
  9. class IndexOfScraperApp:
  10.     def __init__(self, root):
  11.         self.root = root
  12.         self.root.title("Najeeb Shah Khan Index Of Scraper Images and Download")
  13.  
  14.         self.url_label = ttk.Label(root, text="Enter Index Of URL:")
  15.         self.url_entry = ttk.Entry(root, width=100)
  16.         self.scrape_button = ttk.Button(root, text="Scrape Images", command=self.scrape_images)
  17.         self.save_button = ttk.Button(root, text="Save Images", command=self.save_images, state=tk.DISABLED)
  18.  
  19.         self.image_urls = []
  20.  
  21.         self.url_label.grid(row=0, column=0, pady=5)
  22.         self.url_entry.grid(row=0, column=1, pady=5)
  23.         self.scrape_button.grid(row=0, column=2, pady=5)
  24.         self.save_button.grid(row=0, column=3, pady=5)
  25.  
  26.     def scrape_images(self):
  27.         url = self.url_entry.get()
  28.         if not url:
  29.             return
  30.  
  31.         try:
  32.             response = requests.get(url)
  33.             response.raise_for_status()
  34.         except requests.exceptions.RequestException as e:
  35.             self.display_result(f"Error: {e}")
  36.             return
  37.  
  38.         soup = BeautifulSoup(response.content, 'html.parser')
  39.         links = [a['href'] for a in soup.find_all('a', href=True) if a['href'].endswith(('.jpg', '.png'))]
  40.  
  41.         self.image_urls = [urljoin(url, link) for link in links]
  42.  
  43.         if self.image_urls:
  44.             self.display_result(f"Found {len(self.image_urls)} images.")
  45.             self.save_button.config(state=tk.NORMAL)
  46.         else:
  47.             self.display_result("No images found in the directory.")
  48.             self.save_button.config(state=tk.DISABLED)
  49.  
  50.     def save_images(self):
  51.         if not self.image_urls:
  52.             return
  53.  
  54.         directory = filedialog.askdirectory(title="Select Directory to Save Images")
  55.  
  56.         if directory:
  57.             for i, img_url in enumerate(self.image_urls):
  58.                 try:
  59.                     response = requests.get(img_url)
  60.                     response.raise_for_status()
  61.                 except requests.exceptions.RequestException as e:
  62.                     self.display_result(f"Error downloading image {i + 1}: {e}")
  63.                     continue
  64.  
  65.                 _, ext = os.path.splitext(img_url)
  66.                 filename = f"image_{i + 1}{ext}"
  67.                 filepath = os.path.join(directory, filename)
  68.  
  69.                 with open(filepath, 'wb') as f:
  70.                     f.write(response.content)
  71.  
  72.             self.display_result(f"Images saved to {directory}.")
  73.  
  74.     def display_result(self, text):
  75.         result_label = ttk.Label(self.root, text=text)
  76.         result_label.grid(row=1, column=0, columnspan=4, pady=10)
  77.  
  78. if __name__ == "__main__":
  79.     root = tk.Tk()
  80.     app = IndexOfScraperApp(root)
  81.     root.mainloop()
  82.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement