Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
99 lines (72 loc) · 2.65 KB
/
Copy pathmain.py
File metadata and controls
99 lines (72 loc) · 2.65 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
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
from web_scraper import WebScraper
scraper = WebScraper()
while True:
print("\n📚 ADVANCED WEB SCRAPER")
print("1. Scrape Books")
print("2. Search Book")
print("3. Filter by Price")
print("4. Sort by Price")
print("5. Exit")
choice = input("Enter your choice: ").strip()
if choice == "1":
try:
pages = int(input("Enter number of pages: "))
if pages <= 0:
print("Pages must be greater than 0. ❌")
continue
scraper.show_books(pages)
except ValueError:
print("Please enter a valid number. ❌")
elif choice == "2":
keyword = input("Enter book name: ").strip()
if not keyword:
print("Search cannot be empty. ❌")
continue
try:
pages = int(input("Enter number of pages: "))
if pages <= 0:
print("Pages must be greater than 0. ❌")
continue
scraper.search_book(keyword, pages)
except ValueError:
print("Please enter a valid number. ❌")
elif choice == "3":
try:
max_price = float(input("Enter maximum price: "))
pages = int(input("Enter number of pages: "))
if max_price < 0 or pages <= 0:
print("Please enter valid values. ❌")
continue
books = scraper.filter_by_price(max_price, pages)
print(f"\n===== BOOKS UNDER £{max_price:.2f} =====")
if not books:
print("No books found.")
else:
for book in books:
print(
f"{book['title']} → "
f"£{book['price']:.2f}"
)
except ValueError:
print("Please enter valid numbers. ❌")
elif choice == "4":
try:
pages = int(input("Enter number of pages: "))
if pages <= 0:
print("Pages must be greater than 0. ❌")
continue
books = scraper.get_books(pages)
sorted_books = scraper.sort_by_price(books)
print("\n===== 5 CHEAPEST BOOKS =====")
for book in sorted_books[:5]:
print(
f"{book['title']} → "
f"£{book['price']:.2f}"
)
except ValueError:
print("Please enter a valid number. ❌")
elif choice == "5":
print("Goodbye! 👋")
break
else:
print("Invalid choice!")