Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import requests
|
3 |
+
from bs4 import BeautifulSoup
|
4 |
+
from urllib.parse import urljoin, urlparse
|
5 |
+
from zipfile import ZipFile
|
6 |
+
from io import BytesIO
|
7 |
+
|
8 |
+
def download_file(url, session):
|
9 |
+
"""Download a file and return its content."""
|
10 |
+
response = session.get(url)
|
11 |
+
response.raise_for_status()
|
12 |
+
return response.content
|
13 |
+
|
14 |
+
def save_webpage_as_zip(url, output_zip):
|
15 |
+
"""Save a webpage and its assets as a ZIP file."""
|
16 |
+
session = requests.Session()
|
17 |
+
response = session.get(url)
|
18 |
+
response.raise_for_status()
|
19 |
+
|
20 |
+
soup = BeautifulSoup(response.content, 'html.parser')
|
21 |
+
|
22 |
+
# Create a temporary directory to store downloaded files
|
23 |
+
temp_dir = 'temp_webpage'
|
24 |
+
if not os.path.exists(temp_dir):
|
25 |
+
os.makedirs(temp_dir)
|
26 |
+
|
27 |
+
# Download and save the main HTML file
|
28 |
+
main_html_path = os.path.join(temp_dir, 'index.html')
|
29 |
+
with open(main_html_path, 'wb') as f:
|
30 |
+
f.write(response.content)
|
31 |
+
|
32 |
+
# Prepare a list of all assets to download
|
33 |
+
assets = []
|
34 |
+
for tag in soup.find_all(['img', 'link', 'script']):
|
35 |
+
if tag.name == 'img' and tag.get('src'):
|
36 |
+
assets.append(tag['src'])
|
37 |
+
elif tag.name == 'link' and tag.get('href'):
|
38 |
+
assets.append(tag['href'])
|
39 |
+
elif tag.name == 'script' and tag.get('src'):
|
40 |
+
assets.append(tag['src'])
|
41 |
+
|
42 |
+
# Download and save all assets
|
43 |
+
for asset in assets:
|
44 |
+
asset_url = urljoin(url, asset)
|
45 |
+
asset_path = urlparse(asset_url).path.lstrip('/')
|
46 |
+
asset_full_path = os.path.join(temp_dir, asset_path)
|
47 |
+
|
48 |
+
# Create directories if they don't exist
|
49 |
+
os.makedirs(os.path.dirname(asset_full_path), exist_ok=True)
|
50 |
+
|
51 |
+
# Download and save the asset
|
52 |
+
content = download_file(asset_url, session)
|
53 |
+
with open(asset_full_path, 'wb') as f:
|
54 |
+
f.write(content)
|
55 |
+
|
56 |
+
# Create a ZIP file
|
57 |
+
with ZipFile(output_zip, 'w') as zipf:
|
58 |
+
for root, _, files in os.walk(temp_dir):
|
59 |
+
for file in files:
|
60 |
+
file_path = os.path.join(root, file)
|
61 |
+
zipf.write(file_path, os.path.relpath(file_path, temp_dir))
|
62 |
+
|
63 |
+
# Clean up temporary directory
|
64 |
+
for root, _, files in os.walk(temp_dir, topdown=False):
|
65 |
+
for file in files:
|
66 |
+
os.remove(os.path.join(root, file))
|
67 |
+
os.rmdir(root)
|
68 |
+
|
69 |
+
# Example usage
|
70 |
+
url = 'http://example.com'
|
71 |
+
output_zip = 'webpage.zip'
|
72 |
+
save_webpage_as_zip(url, output_zip)
|