-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathText-Based_Browser.py
More file actions
66 lines (58 loc) · 1.78 KB
/
Text-Based_Browser.py
File metadata and controls
66 lines (58 loc) · 1.78 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
56
57
58
59
60
61
62
63
64
65
66
import os
import sys
import requests
from bs4 import BeautifulSoup
from colorama import Fore, Style
def get_url():
link = input().lower()
if link != "back" and link != "exit" and "https://" not in link and "http://" not in link:
return f"https://{link}"
else:
return link
def get_filename(site_url):
filename = ""
if site_url.startswith("https://"):
filename = site_url[8:]
if site_url.startswith("http://"):
filename = site_url[7:]
if site_url.startswith("www."):
filename = site_url[4:]
if "/" in filename:
filename = filename.replace("/", ".")
return filename
def print_user_view(r):
soup = BeautifulSoup(r.content, 'html.parser')
text = soup.find_all(["p", "a", "ul", "ol", "li"])
output = []
for line in text:
output.append(line.text)
if line.name == "a":
print(Fore.BLUE + line.text + Style.RESET_ALL)
else:
print(line.text)
return output
directory_name = sys.argv[1]
pages_history = []
try:
os.mkdir(directory_name)
except FileExistsError:
print("The directory already exist.")
else:
print("Directory created successfully.")
while True:
url = get_url()
if url == "back" and pages_history:
pages_history.pop()
url = pages_history.pop()
if "." in url:
pages_history.append(url)
request = requests.get(url)
page_name = get_filename(url)
page_output = print_user_view(request)
with open(f"{directory_name}/{page_name}", "w", encoding="utf-8") as file:
for text_line in page_output:
file.write(text_line)
elif url == "exit":
break
else:
print("Error: wrong url.")