-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathText-Based Browser.py
More file actions
46 lines (41 loc) · 1.32 KB
/
Copy pathText-Based Browser.py
File metadata and controls
46 lines (41 loc) · 1.32 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
import os, sys, requests
from bs4 import BeautifulSoup
from colorama import init,Fore
init(autoreset=True)
try:
os.mkdir(sys.argv [1])
except:
pass
stack = []
def read_or_write_site(filename, mode='w'):
url = requests.get('https://' + filename).text
soup = BeautifulSoup(url, 'html.parser')
elem = soup.find_all(['p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'ol', 'ul', 'li', 'a','span'])
with open(os.path.join(os.getcwd(), sys.argv[1], f'{filename.rsplit(".", 1)[0]}.txt'), mode) as f:
if mode == 'w':
stack.append(url.rsplit(".", 1)[0])
for i in elem:
f.write(i.get_text().replace('\n', ''))
if i.name=='a':
print(Fore.BLUE + i.get_text().replace('\n', ''))
else:
print(i.get_text().replace('\n', ''))
else:
print(f.read())
while True:
url = input("\n")
if "." in url:
if requests.get('https://' + url):
read_or_write_site(url)
elif url.rsplit('.', 1)[0] in stack:
read_or_write_site(url, mode='r')
elif url == 'back':
try:
stack.pop()
read_or_write_site(stack[-1], mode='r')
except IndexError:
continue
elif url == 'exit':
break
else:
print('Error: Incorrect URL')