-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebscraper+v1.py
298 lines (251 loc) · 12.3 KB
/
webscraper+v1.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import os
import time
import requests
import undetected_chromedriver as uc
import chromedriver_autoinstaller
from bs4 import BeautifulSoup
from urllib.parse import urljoin, urlparse
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from colorama import init, Style
init()
def rgb_to_ansi(r, g, b):
return f'\033[38;2;{r};{g};{b}m'
def color_fade(text, start_color, end_color):
def interpolate(start, end, factor):
return int(start + (end - start) * factor)
start_rgb = tuple(int(start_color[i:i+2], 16) for i in (0, 2, 4))
end_rgb = tuple(int(end_color[i:i+2], 16) for i in (0, 2, 4))
gradient_text = ""
for i, char in enumerate(text):
factor = i / (len(text) - 1)
color_rgb = tuple(interpolate(start, end, factor) for start, end in zip(start_rgb, end_rgb))
gradient_text += rgb_to_ansi(*color_rgb) + char
return gradient_text + Style.RESET_ALL
def set_console_title(title):
while True:
for i in range(len(title)):
os.system(f"title {title[:i+1]}")
time.sleep(0.1)
time.sleep(1)
for i in range(len(title), 0, -1):
os.system(f"title {title[:i-1]}")
time.sleep(0.05)
time.sleep(1)
def setup_chrome_driver():
chromedriver_path = chromedriver_autoinstaller.install()
if not chromedriver_path:
print("Failed to install ChromeDriver.")
return None
options = uc.ChromeOptions()
options.add_argument("--ignore-certificate-errors")
options.add_argument("--disable-web-security")
options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage")
options.headless = False
try:
driver = uc.Chrome(options=options, driver_executable_path=chromedriver_path)
return driver
except Exception as e:
print(f"Error setting up ChromeDriver: {e}")
return None
def main():
import threading
title_thread = threading.Thread(target=set_console_title, args=("WebScraper+ By Germanized",), daemon=True)
title_thread.start()
url = input(color_fade("Enter the URL of the website you want to copy: ", "00ffff", "0000ff")).strip()
if not urlparse(url).scheme:
url = 'http://' + url
github_pages = input(color_fade("Would you like this to be GitHub Pages compatible? (Y/N): ", "00ffff", "0000ff")).strip().lower()
github_pages_compatible = github_pages == 'y'
manual_visit = input(color_fade("Would you like to manually visit the website? (Y/N): ", "00ffff", "0000ff")).strip().lower() == 'y'
start_scraping = not manual_visit and input(color_fade("Would you like to start scraping after typing 'scrape' in the cmd? (Y/N): ", "00ffff", "0000ff")).strip().lower() == 'y'
base_directory = os.path.join(os.path.dirname(os.path.abspath(__file__)), urlparse(url).netloc)
print(color_fade("Opening the website. Type 'scrape' and press Enter to start scraping.", "00ffff", "0000ff"))
if manual_visit:
driver = setup_chrome_driver()
if driver:
try:
driver.get(url)
print(color_fade(f"Website opened. Manually inspect the site. Type 'scrape' and press Enter to start scraping.", "00ffff", "0000ff"))
while True:
command = input(color_fade("> ", "00ffff", "0000ff")).strip().lower()
if command == 'scrape':
save_content(url, base_directory, github_pages_compatible)
break
else:
print(color_fade("Invalid command. Type 'scrape' to start scraping.", "ff0000", "000000"))
finally:
driver.quit()
else:
driver = None
max_retries = 5
retry_delay = 5
retry_count = 0
while retry_count < max_retries:
try:
driver = setup_chrome_driver()
if driver:
driver.get(url)
print(color_fade("Page loaded. Type 'scrape' to start scraping.", "00ffff", "0000ff"))
if start_scraping:
while True:
command = input(color_fade("> ", "00ffff", "0000ff")).strip().lower()
if command == 'scrape':
save_content(url, base_directory, github_pages_compatible)
break
else:
print(color_fade("Invalid command. Type 'scrape' to start scraping.", "ff0000", "000000"))
break
except Exception as e:
if "ERR_SSL_PROTOCOL_ERROR" in str(e):
print(f"SSL error occurred: {e}. Retrying in {retry_delay} seconds...")
else:
print(f"Error occurred: {e}. Retrying in {retry_delay} seconds...")
time.sleep(retry_delay)
retry_count += 1
finally:
if driver:
driver.quit()
if retry_count >= max_retries:
print(color_fade("Failed to open the website after several attempts.", "ff0000", "000000"))
def fetch_page_source(driver):
try:
print("Page loaded. Type 'scrape' and press Enter to start scraping.")
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.TAG_NAME, "body")))
return driver.page_source
except Exception as e:
print(f"Error fetching page source: {e}")
return None
def save_content(url, directory, github_pages_compatible):
if not urlparse(url).scheme:
url = 'http://' + url
print(color_fade("Fetching content from: ", "00ffff", "0000ff") + url)
if not os.path.exists(directory):
os.makedirs(directory)
driver = None
page_source = None
try:
driver = setup_chrome_driver()
if driver is None:
return
driver.get(url)
page_source = fetch_page_source(driver)
if page_source:
soup = BeautifulSoup(page_source, 'html.parser')
if github_pages_compatible:
save_github_pages_compatible(soup, directory, url)
else:
save_html(soup, directory, url)
save_assets(soup, directory, url)
else:
print(color_fade("Failed to retrieve page source.", "ff0000", "000000"))
finally:
if driver:
driver.quit()
def save_github_pages_compatible(soup, directory, url):
html_content = str(soup)
js_files = [js.get('src') for js in soup.find_all('script') if js.get('src')]
css_files = [css.get('href') for css in soup.find_all('link', {'rel': 'stylesheet'}) if css.get('href')]
with open(os.path.join(directory, 'index.html'), 'w', encoding='utf-8') as f:
f.write(html_content)
print(color_fade(f"Saved GitHub Pages-compatible HTML: {os.path.join(directory, 'index.html')}", "00ffff", "0000ff"))
for js_url in js_files:
save_asset(urljoin(url, js_url), os.path.join(directory, 'scripts.js'))
for css_url in css_files:
save_asset(urljoin(url, css_url), os.path.join(directory, 'styles.css'))
def save_html(soup, directory, url):
parsed_url = urlparse(url)
filename = 'index.html' if parsed_url.path == '/' else os.path.basename(parsed_url.path)
filepath = os.path.join(directory, filename)
with open(filepath, 'w', encoding='utf-8') as f:
f.write(str(soup))
print(color_fade(f"Saved HTML: {filepath}", "00ffff", "0000ff"))
def save_assets(soup, directory, base_url):
tags = soup.find_all(['img', 'link', 'script'])
for tag in tags:
url_attr = 'src' if tag.name == 'script' else 'href' if tag.name == 'link' else 'src'
asset_url = tag.get(url_attr)
if asset_url:
asset_url = urljoin(base_url, asset_url)
save_asset(asset_url, directory)
def save_asset(url, directory):
parsed_url = urlparse(url)
asset_path = os.path.join(directory, parsed_url.netloc, parsed_url.path.lstrip('/'))
asset_dir = os.path.dirname(asset_path)
if not os.path.exists(asset_dir):
os.makedirs(asset_dir)
try:
response = requests.get(url)
if response.status_code == 200:
with open(asset_path, 'wb') as f:
f.write(response.content)
print(color_fade(f"Saved asset: {asset_path}", "00ffff", "0000ff"))
else:
print(color_fade(f"Failed to retrieve asset: {url}", "ff0000", "000000"))
except requests.exceptions.SSLError as e:
print(color_fade(f"SSL error while retrieving asset: {url}", "ff0000", "000000"))
except Exception as e:
print(color_fade(f"Error while retrieving asset: {url}", "ff0000", "000000"))
def main():
import threading
title_thread = threading.Thread(target=set_console_title, args=("WebScraper+ By Germanized",), daemon=True)
title_thread.start()
url = input(color_fade("Enter the URL of the website you want to copy: ", "00ffff", "0000ff")).strip()
if not urlparse(url).scheme:
url = 'http://' + url
github_pages = input(color_fade("Would you like this to be GitHub Pages compatible? (Y/N): ", "00ffff", "0000ff")).strip().lower()
github_pages_compatible = github_pages == 'y'
manual_visit = input(color_fade("Would you like to manually visit the website? (Y/N): ", "00ffff", "0000ff")).strip().lower() == 'y'
start_scraping = not manual_visit and input(color_fade("Would you like to start scraping after typing 'scrape' in the cmd? (Y/N): ", "00ffff", "0000ff")).strip().lower() == 'y'
base_directory = os.path.join(os.path.dirname(os.path.abspath(__file__)), urlparse(url).netloc)
print(color_fade("Opening the website. Type 'scrape' and press Enter to start scraping.", "00ffff", "0000ff"))
if manual_visit:
driver = setup_chrome_driver()
if driver:
try:
driver.get(url)
print(color_fade(f"Website opened. Manually inspect the site. Type 'scrape' and press Enter to start scraping.", "00ffff", "0000ff"))
while True:
command = input(color_fade("> ", "00ffff", "0000ff")).strip().lower()
if command == 'scrape':
save_content(url, base_directory, github_pages_compatible)
break
else:
print(color_fade("Invalid command. Type 'scrape' to start scraping.", "ff0000", "000000"))
finally:
driver.quit()
else:
driver = None
max_retries = 5
retry_delay = 5
retry_count = 0
while retry_count < max_retries:
try:
driver = setup_chrome_driver()
if driver:
driver.get(url)
print(color_fade("Page loaded. Type 'scrape' to start scraping.", "00ffff", "0000ff"))
if start_scraping:
while True:
command = input(color_fade("> ", "00ffff", "0000ff")).strip().lower()
if command == 'scrape':
save_content(url, base_directory, github_pages_compatible)
break
else:
print(color_fade("Invalid command. Type 'scrape' to start scraping.", "ff0000", "000000"))
break
except Exception as e:
print(f"Error occurred: {e}. Retrying in {retry_delay} seconds...")
time.sleep(retry_delay)
retry_count += 1
finally:
if driver:
driver.quit()
if retry_count >= max_retries:
print(color_fade("Failed to open the website after several attempts.", "ff0000", "000000"))
if __name__ == "__main__":
main()