-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtml_reader.py
56 lines (50 loc) · 1.62 KB
/
html_reader.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
from bs4 import BeautifulSoup as beautiful_soup
def open_up(fp):
with open(fp, 'r') as contents:
global soup
global text
soup = beautiful_soup(contents, 'html.parser')
text = soup.get_text()
html_menu()
def search():
search_entry = input("Search: ")
results = []
for line in text.split('\n'):
if search_entry.lower() in line.lower():
results.append(line.strip())
if results:
print("Search results:")
for result in results:
print(result,'\n')
else:
print("No results found.")
def extract_links():
links = set()
for link in soup.find_all('a', href=True):
link_text=link['href']
link_text = link_text[:39] #Truncate the link to the first 40 characters to remove very similar links
links.add(link_text)
for items in links:
print(items, '\n')
def html_menu():
user_input = input("What would you like to do? \n 1) Show HTML \n 2) Show plain text \n 3) Show links \n 4) Search \n 0) Exit \n ")
if user_input.strip() == "1":
print(soup.prettify())
print("Scroll up for results.")
html_menu()
if user_input.strip() == "2":
print(text)
print("Scroll up for results.")
html_menu()
if user_input.strip() == "3":
extract_links()
print("Links that differ beyond 40 characters not shown.")
html_menu()
if user_input.strip() == "4":
search()
html_menu()
if user_input.strip() == "0":
quit()
else:
print("Please enter a valid number:")
html_menu()