-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmian.py
43 lines (30 loc) · 1.33 KB
/
mian.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
from bs4 import BeautifulSoup
import requests
import re
search_term = input("What product are you searching for?")
url = f"https://www.newegg.ca/p/pl?d={search_term}&N=4131"
page = requests.get(url).text
doc = BeautifulSoup(page, "html.parser")
page_text = doc.find(class_="list-tool-pagination-text").strong
pages = int(str(page_text).split("/")[-2].split(">")[-1][:-1])
items_found = {}
for page in range(1, pages + 1):
url = f"https://www.newegg.ca/p/pl?d={search_term}&N=4131&page={page}"
page = requests.get(url).text
doc = BeautifulSoup(page, "html.parser")
div = doc.find(class_="item-cells-wrap border-cells items-grid-view four-cells expulsion-one-cell")
items = div.find_all(text=re.compile(search_term))
for item in items:
parent = item.parent
if parent.name != "a":
continue
link = parent['href']
next_parent = item.find_parent(class_="item-container")
price = next_parent.find(class_="price-current").strong.string
items_found[item] = {"price": int(price.replace(",", "")), "link": link}
sorted_items = sorted(items_found.items(), key=lambda x: x[1]['price'])
for item in sorted_items:
print(item[0])
print(f"${item[1]['price']}")
print(item[1]['link'])
print("-----------------------------------------------------------------")