File tree Expand file tree Collapse file tree 3 files changed +56
-1
lines changed Expand file tree Collapse file tree 3 files changed +56
-1
lines changed Original file line number Diff line number Diff line change 1
1
* .csv
2
2
* .xlsx
3
3
* .json
4
- * .env
4
+ * .env
5
+ env
6
+
Original file line number Diff line number Diff line change
1
+ images
Original file line number Diff line number Diff line change
1
+ import os
2
+ import requests
3
+ from bs4 import BeautifulSoup
4
+ from urllib .parse import urljoin , urlparse
5
+
6
+ def download_images (url , folder = "images" ):
7
+ # Create folder if it doesn't exist
8
+ os .makedirs (folder , exist_ok = True )
9
+
10
+ # Get page content
11
+ response = requests .get (url )
12
+ if response .status_code != 200 :
13
+ print (f"Failed to retrieve URL: { url } " )
14
+ return
15
+
16
+ # Parse HTML
17
+ soup = BeautifulSoup (response .text , "html.parser" )
18
+
19
+ # Find all image tags
20
+ img_tags = soup .find_all ("img" )
21
+ if not img_tags :
22
+ print ("No images found on the page." )
23
+ return
24
+
25
+ for img in img_tags :
26
+ img_url = img .get ("src" )
27
+ if not img_url :
28
+ continue
29
+
30
+ # Convert relative URLs to absolute
31
+ img_url = urljoin (url , img_url )
32
+
33
+ # Get image filename
34
+ parsed_url = urlparse (img_url )
35
+ img_name = os .path .basename (parsed_url .path )
36
+
37
+ if not img_name :
38
+ continue
39
+
40
+ # Download image
41
+ img_data = requests .get (img_url ).content
42
+ img_path = os .path .join (folder , img_name )
43
+
44
+ # Save image
45
+ with open (img_path , "wb" ) as f :
46
+ f .write (img_data )
47
+
48
+ print (f"Downloaded: { img_name } " )
49
+
50
+ # Example usage
51
+ website_url = "https://themeholy.com/html/agenxe/demo/index.html" # Replace with your target URL
52
+ download_images (website_url )
You can’t perform that action at this time.
0 commit comments