python code to get the source code for a spasific site in folder ☕
import requests
from bs4 import BeautifulSoup
import os
def get_source_code(url):
try:
response = requests.get(url)
response.raise_for_status() # Raise an exception for HTTP errors
return response.text
except requests.exceptions.RequestException as e:
print(f"An error occurred: {e}")
return None
def save_file(content, file_path):
with open(file_path, 'w', encoding='utf-8') as file:
file.write(content)
def extract_and_save_resources(url):
# Fetch the HTML content
html_content = get_source_code(url)
if not html_content:
return
# Save the HTML content
html_file_path = 'index.html'
save_file(html_content, html_file_path)
print(f"HTML content saved to {html_file_path}")
# Parse the HTML content
soup = BeautifulSoup(html_content, 'html.parser')
# Extract and save CSS files
for link in soup.find_all('link', rel='stylesheet'):
css_url = link.get('href')
if css_url:
css_content = get_source_code(css_url)
if css_content:
css_file_path = os.path.join('css', os.path.basename(css_url))
os.makedirs(os.path.dirname(css_file_path), exist_ok=True)
save_file(css_content, css_file_path)
print(f"CSS content saved to {css_file_path}")
# Extract and save JavaScript files
for script in soup.find_all('script', src=True):
js_url = script.get('src')
if js_url:
js_content = get_source_code(js_url)
if js_content:
js_file_path = os.path.join('js', os.path.basename(js_url))
os.makedirs(os.path.dirname(js_file_path), exist_ok=True)
save_file(js_content, js_file_path)
print(f"JS content saved to {js_file_path}")
# Example usage
url = 'https://example.com'
extract_and_save_resources(url)