-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb_scraper.py
More file actions
100 lines (73 loc) · 2.91 KB
/
Copy pathweb_scraper.py
File metadata and controls
100 lines (73 loc) · 2.91 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
100
import requests
from bs4 import BeautifulSoup
class WebScraper:
def get_books(self, pages=1):
books = []
for page in range(1, pages + 1):
url = f"https://books.toscrape.com/catalogue/page-{page}.html"
try:
response = requests.get(url, timeout=10)
if response.status_code != 200:
print(f"Page {page} could not be loaded. ❌")
continue
soup = BeautifulSoup(response.text, "html.parser")
for book in soup.find_all(
"article",
class_="product_pod"
):
title = book.find("h3").find("a")["title"]
price_text = book.find(
"p",
class_="price_color"
).get_text(strip=True)
price = float(
price_text.replace("£", "").replace("£", "")
)
rating_class = book.find(
"p",
class_="star-rating"
)["class"]
rating = rating_class[1]
books.append({
"title": title,
"price": price,
"rating": rating
})
except requests.RequestException:
print(f"Network error on page {page}. ❌")
return books
def search_book(self, keyword, pages=1):
books = self.get_books(pages)
found = False
for book in books:
if keyword.lower() in book["title"].lower():
print("\n===== BOOK FOUND =====")
print(f"Title : {book['title']}")
print(f"Price : £{book['price']:.2f}")
print(f"Rating : {book['rating']}")
found = True
if not found:
print("No matching books found.")
def filter_by_price(self, max_price, pages=1):
books = self.get_books(pages)
filtered_books = []
for book in books:
if book["price"] <= max_price:
filtered_books.append(book)
return filtered_books
def sort_by_price(self, books, descending=False):
return sorted(
books,
key=lambda book: book["price"],
reverse=descending
)
def show_books(self, pages=1):
books = self.get_books(pages)
if not books:
print("No books found.")
return
print(f"\n===== {len(books)} BOOKS =====")
for index, book in enumerate(books, start=1):
print(f"\n{index}. {book['title']}")
print(f" Price : £{book['price']:.2f}")
print(f" Rating : {book['rating']}")