Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import tkinter as tk
- from tkinter import ttk
- from tkinter import filedialog
- import requests
- from bs4 import BeautifulSoup
- from urllib.parse import urljoin
- class IndexOfScraperApp:
- def __init__(self, root):
- self.root = root
- self.root.title("Najeeb Shah Khan Index Of Scraper Images and Download")
- self.url_label = ttk.Label(root, text="Enter Index Of URL:")
- self.url_entry = ttk.Entry(root, width=100)
- self.scrape_button = ttk.Button(root, text="Scrape Images", command=self.scrape_images)
- self.save_button = ttk.Button(root, text="Save Images", command=self.save_images, state=tk.DISABLED)
- self.image_urls = []
- self.url_label.grid(row=0, column=0, pady=5)
- self.url_entry.grid(row=0, column=1, pady=5)
- self.scrape_button.grid(row=0, column=2, pady=5)
- self.save_button.grid(row=0, column=3, pady=5)
- def scrape_images(self):
- url = self.url_entry.get()
- if not url:
- return
- try:
- response = requests.get(url)
- response.raise_for_status()
- except requests.exceptions.RequestException as e:
- self.display_result(f"Error: {e}")
- return
- soup = BeautifulSoup(response.content, 'html.parser')
- links = [a['href'] for a in soup.find_all('a', href=True) if a['href'].endswith(('.jpg', '.png'))]
- self.image_urls = [urljoin(url, link) for link in links]
- if self.image_urls:
- self.display_result(f"Found {len(self.image_urls)} images.")
- self.save_button.config(state=tk.NORMAL)
- else:
- self.display_result("No images found in the directory.")
- self.save_button.config(state=tk.DISABLED)
- def save_images(self):
- if not self.image_urls:
- return
- directory = filedialog.askdirectory(title="Select Directory to Save Images")
- if directory:
- for i, img_url in enumerate(self.image_urls):
- try:
- response = requests.get(img_url)
- response.raise_for_status()
- except requests.exceptions.RequestException as e:
- self.display_result(f"Error downloading image {i + 1}: {e}")
- continue
- _, ext = os.path.splitext(img_url)
- filename = f"image_{i + 1}{ext}"
- filepath = os.path.join(directory, filename)
- with open(filepath, 'wb') as f:
- f.write(response.content)
- self.display_result(f"Images saved to {directory}.")
- def display_result(self, text):
- result_label = ttk.Label(self.root, text=text)
- result_label.grid(row=1, column=0, columnspan=4, pady=10)
- if __name__ == "__main__":
- root = tk.Tk()
- app = IndexOfScraperApp(root)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement