Surya_from_1627

WSP8

Oct 2nd, 2026
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.01 KB | None | 0 0
  1. import requests
  2. from bs4 import BeautifulSoup
  3. from urllib.parse import urljoin
  4.  
  5. def extract_image_links(url):
  6.     try:
  7.         headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'}
  8.         response = requests.get(url, headers=headers)
  9.  
  10.         response.raise_for_status()
  11.  
  12.         soup = BeautifulSoup(response.text, 'html.parser')
  13.         images = soup.find_all('img')
  14.  
  15.         print(f"Image links found on: {url}\n")
  16.         if not images:
  17.             print("No image tags found.")
  18.             return
  19.  
  20.         for img in images:
  21.             src = img.get('src')
  22.             if src:
  23.                 # Convert relative URLs to absolute
  24.                 full_url = urljoin(url, src)
  25.                 print(full_url)
  26.  
  27.     except requests.exceptions.RequestException as e:
  28.         print(f"Error fetching the webpage: {e}")
  29.  
  30. # Example usage
  31. website_url = input("Enter the website URL: ")
  32. extract_image_links(website_url)
  33.  
Advertisement
Add Comment
Please, Sign In to add comment