-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathimot.bg-download
executable file
·52 lines (43 loc) · 1.7 KB
/
imot.bg-download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env python3
import sys
import os
import urllib.request
import re
def create_opener():
user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
headers = {'User-Agent': user_agent}
opener = urllib.request.build_opener()
opener.addheaders = [('User-Agent', user_agent)]
return opener
def download_images(url, output_dir='.'):
opener = create_opener()
urllib.request.install_opener(opener)
# Fetch the webpage content
try:
with urllib.request.urlopen(url) as response:
html_content = response.read().decode('windows-1251')
except urllib.error.HTTPError as e:
print(f"Error fetching the webpage: {e}")
return
# Find all URLs matching the pattern
pattern = r'https://imotstatic\d\.focus\.bg/imot/photosimotbg/\S+?/big1/\S+?\.\w+'
image_urls = list(set(re.findall(pattern, html_content)))
# Create the output directory if it doesn't exist
os.makedirs(output_dir, exist_ok=True)
# Download images
for img_url in image_urls:
filename = os.path.join(output_dir, os.path.basename(img_url))
print(f"Downloading: {img_url}")
try:
urllib.request.urlretrieve(img_url, filename)
except urllib.error.URLError as e:
print(f"Error downloading {img_url}: {e}")
return len(image_urls)
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: python script.py <URL> [output_directory]")
sys.exit(1)
url = sys.argv[1]
output_dir = sys.argv[2] if len(sys.argv) > 2 else '.'
total = download_images(url, output_dir)
print(f"Download complete (total {total}).")