How to Make Your Python Web Scraper More Efficient | Learn Web Scraping in 2025
Demo :
Click Video πππ
Code :
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
import requests
from bs4 import BeautifulSoup
# Function to scrape the data
def web_scraper():
url = url_entry.get()
if not url:
messagebox.showerror("Input Error", "Please enter a URL!")
return
try:
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
# Extracting the title
title = soup.title.string if soup.title else "No title found"
title_label.config(text=f"Page Title: {title}")
# Extracting all anchor tags (links)
links = soup.find_all('a')
links_text = ""
for link in links:
href = link.get('href')
if href:
links_text += href + "\n"
if links_text:
links_textbox.config(state=tk.NORMAL)
links_textbox.delete(1.0, tk.END)
links_textbox.insert(tk.END, links_text)
links_textbox.config(state=tk.DISABLED)
else:
links_textbox.config(state=tk.NORMAL)
links_textbox.delete(1.0, tk.END)
links_textbox.insert(tk.END, "No links found.")
links_textbox.config(state=tk.DISABLED)
except requests.exceptions.RequestException as e:
messagebox.showerror("Error", f"Error occurred: {e}")
# GUI setup
root = tk.Tk()
root.title("Python Web Scraper")
root.geometry("600x400")
root.config(bg="#2e3b4e")
# Title label
title_label = tk.Label(root, text="Enter a URL to Scrape", font=("Arial", 16), fg="white", bg="#2e3b4e")
title_label.pack(pady=10)
# URL entry field
url_entry = tk.Entry(root, font=("Arial", 14), width=40)
url_entry.pack(pady=10)
# Scrape button
scrape_button = tk.Button(root, text="Scrape", font=("Arial", 14), bg="#4CAF50", fg="white", command=web_scraper)
scrape_button.pack(pady=20)
# Result labels
title_label = tk.Label(root, text="Page Title: ", font=("Arial", 12), fg="white", bg="#2e3b4e")
title_label.pack(pady=10)
# Links Textbox
links_textbox = tk.Text(root, font=("Arial", 12), width=60, height=8, wrap=tk.WORD, bg="#f1f1f1", fg="black", state=tk.DISABLED)
links_textbox.pack(pady=10)
# Run the GUI loop
root.mainloop()
Comments
Post a Comment