-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscrape_css.py
More file actions
55 lines (43 loc) · 1.6 KB
/
Copy pathscrape_css.py
File metadata and controls
55 lines (43 loc) · 1.6 KB
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
53
54
55
import requests
from bs4 import BeautifulSoup
from urllib.parse import urljoin
import os
# Target URL
url = "https://didierlopes.com"
# Create directory for CSS files
os.makedirs("scraped_css", exist_ok=True)
# 1. Fetch the HTML
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
# 2. Find all CSS links
css_links = []
for link in soup.find_all("link", rel="stylesheet"):
if link.get('href'):
css_links.append(link['href'])
print(f"Found {len(css_links)} CSS files:")
for link in css_links:
print(f" - {link}")
# 3. Download each file
for i, link in enumerate(css_links):
# Handle relative URLs (e.g., "/styles/main.css")
full_url = urljoin(url, link)
try:
css_response = requests.get(full_url)
filename = f"scraped_css/style_{i}_{full_url.split('/')[-1]}" or f"scraped_css/style_{i}.css"
# Save to file
with open(filename, "w", encoding="utf-8") as f:
f.write(css_response.text)
print(f"Downloaded: {filename}")
except Exception as e:
print(f"Failed to download {full_url}: {e}")
# Also extract inline styles
print("\nExtracting inline styles...")
inline_styles = []
for style_tag in soup.find_all("style"):
if style_tag.string:
inline_styles.append(style_tag.string)
if inline_styles:
with open("scraped_css/inline_styles.css", "w", encoding="utf-8") as f:
f.write("\n\n/* ===== INLINE STYLES ===== */\n\n".join(inline_styles))
print("Saved inline styles to scraped_css/inline_styles.css")
print(f"\nScraping complete! Check the 'scraped_css' directory.")