forked from Py-Contributors/awesomeScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
website_cloner.py
70 lines (60 loc) · 2.03 KB
/
website_cloner.py
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
""""
Program name : Website cloner
author : https://github.com/codeperfectplus
How to use : Check README.md
"""
import os
import sys
import requests
from bs4 import BeautifulSoup
class CloneWebsite:
def __init__(self, website_name):
self.website_name = website_name
def crawl_website(self):
""" This function will crawl website and return content"""
content = requests.get(website_name)
if content.status_code == 200:
return content
def create_folder(self):
""" This funtion will create folder for website """
folder_name = (website_name.split("/"))[2]
try:
os.makedirs(folder_name)
except Exception as e:
print(e)
return folder_name
def save_website(self):
""" This function will save website to respective folder """
folder_name = self.create_folder()
content = self.crawl_website()
with open(
f"{folder_name}/index.html", "w", encoding="ascii", errors="ignore"
) as file:
file.write(content.text)
def save_image(self):
folder_name = self.create_folder()
os.chdir(folder_name)
data = requests.get(website_name).text
soup = BeautifulSoup(data, "html.parser")
for img in soup.find_all("img"):
src = img["src"]
print(src)
image_name = src.split("/")[-1]
path = src.split("/")[:-1]
path = "/".join(path)
try:
os.makedirs(path)
except Exception:
print("File Exists")
if "/" == src[:1]:
print(src)
src = website_name + src
img_data = requests.get(src).content
with open(f"{path}/{image_name}", "wb") as file:
file.write(img_data)
print("complete")
if __name__ == "__main__":
website_name = sys.argv[1]
clone = CloneWebsite(website_name)
clone.save_website()
clone.save_image()