Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- from bs4 import BeautifulSoup
- from urllib.parse import urljoin
- def extract_image_links(url):
- try:
- 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'}
- response = requests.get(url, headers=headers)
- response.raise_for_status()
- soup = BeautifulSoup(response.text, 'html.parser')
- images = soup.find_all('img')
- print(f"Image links found on: {url}\n")
- if not images:
- print("No image tags found.")
- return
- for img in images:
- src = img.get('src')
- if src:
- # Convert relative URLs to absolute
- full_url = urljoin(url, src)
- print(full_url)
- except requests.exceptions.RequestException as e:
- print(f"Error fetching the webpage: {e}")
- # Example usage
- website_url = input("Enter the website URL: ")
- extract_image_links(website_url)
Advertisement
Add Comment
Please, Sign In to add comment