Skip to content

Commit 8e24c0a

Browse files
committed
update
1 parent 990cce0 commit 8e24c0a

File tree

10 files changed

+334
-29
lines changed

10 files changed

+334
-29
lines changed

FunPy02_SanDealTiki/DealHunter.py

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
from threading import Thread
2+
from Helper import getItemFromHtmlElement
23
import time
34
import random
4-
5+
import requests
6+
from bs4 import BeautifulSoup
57

68
from Target import Target
79

810
class DealHunter (Thread):
9-
DELAY_TIME = 0.2
11+
DELAY_TIME = 10
1012
LOOP_COUNT = 10
1113

1214
def __init__(self, targetItem):
1315
Thread.__init__(self)
14-
self.item = targetItem
15-
self.name = self.item.name
16-
self.foundUrl = ""
16+
self.target = targetItem
17+
self.name = self.target.name
1718
self.status = "...INIT..."
1819

1920

@@ -22,17 +23,27 @@ def start(self):
2223
self.status = "...STARTED..."
2324
return super().start()
2425

26+
def findTheBestDeal(self):
27+
req = requests.get(self.target.url)
28+
soup = BeautifulSoup(req.text, "lxml")
29+
htmlElements = soup.findAll("a", {"class": "search-a-product-item"})
30+
31+
listItem = []
32+
for htmlElement in htmlElements:
33+
newItem = getItemFromHtmlElement(htmlElement)
34+
if self.target.name in newItem.name and newItem.price < self.target.maxPrice:
35+
listItem.append(newItem)
36+
37+
print(self.target.name + ": " + str(len(listItem)) + " items:")
38+
for item in listItem:
39+
# print(self.target.name + ": " + str(item.price) + "\n" + item.url)
40+
print(self.target.name + ": " + str(item.price))
2541

2642
def run(self):
2743
i = 0
2844
self.status = "...RUNNING..."
2945
while True:
30-
i += 1
31-
# print(self.name + " - " + str(i))
32-
# time.sleep(random.random())
46+
self.findTheBestDeal()
3347
time.sleep(DealHunter.DELAY_TIME)
34-
if i > random.randrange(10, 30): #DealHunter.LOOP_COUNT
35-
break
36-
# print("=== [" + self.name +"] FINISHED ===")
37-
self.foundUrl = "...FINISHED..."
38-
self.status = self.foundUrl
48+
49+
self.status = "...FINISHED..."

FunPy02_SanDealTiki/Helper.py

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from threading import Thread
22
from Target import Target
3+
from Item import Item
34
from os import system, name
45
import time
56

@@ -16,12 +17,42 @@ def getItems(fileName):
1617
# print(lines[i])
1718
if(len(lines) == 0):
1819
break
19-
item = Target(lines[i].strip(), lines[i+1].strip(), lines[i+2].strip())
20+
item = Target(lines[i].strip(), correctNumber(lines[i+1].strip()), lines[i+2].strip())
2021
listItem.append(item)
2122
i += 3
2223

2324
return listItem
2425

26+
def correctNumber(price):
27+
price = price.replace('.', '')
28+
price = price.replace('đ', '')
29+
return int(price)
30+
31+
def getItemFromHtmlElement(itemHtml):
32+
newItem = Item()
33+
# 1. Get title
34+
newItem.name = itemHtml.get("title")
35+
# 2. Get final price
36+
priceText = itemHtml.find("span", {"class":"final-price"}).get_text()
37+
# 3. Get regular price (If has)
38+
regularPrice = itemHtml.find("span", {"class":"price-regular"})
39+
if(regularPrice != None):
40+
newItem.regularPrice = correctNumber(regularPrice.get_text().strip())
41+
42+
finalPrice = str(priceText).split("đ")
43+
if(len(finalPrice) > 0): # discount if has
44+
newItem.price = correctNumber(finalPrice[0].strip())
45+
newItem.discount = finalPrice[1].strip()
46+
else:
47+
newItem.price = correctNumber(priceText)
48+
newItem.regularPrice = 0
49+
newItem.discount = ""
50+
51+
# 4. url
52+
url = "https://tiki.vn" + itemHtml.get("href")
53+
newItem.url = url
54+
return newItem
55+
2556

2657
class Display(Thread):
2758

@@ -43,8 +74,4 @@ def isRuning(self):
4374
return bIsRunning
4475

4576
def run(self):
46-
while self.isRuning():
47-
self.display()
48-
time.sleep(0.5)
49-
system("clear")
50-
self.display()
77+
pass

FunPy02_SanDealTiki/Item.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Item:
2+
def __init_(self, name="", price=0, url=""):
3+
self.name = name
4+
self.price = price
5+
self.url = url
6+
self.discount = ""
7+
self.regularPrice = ""
8+
9+
def printInfo(self):
10+
print(self.name + ": " + self.price + "(" + self.discount + ") " + self.regularPrice)

FunPy02_SanDealTiki/Target.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class Target:
2-
def __init__(self, name = "N/A", maxPrice = 1000, url = "None"):
2+
def __init__(self, name = "N/A", maxPrice = 10000000, url = "None"):
33
self.name = name
44
self.url = url
55
self.maxPrice = maxPrice

FunPy02_SanDealTiki/Test.py

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,30 @@
44
URL = "https://tiki.vn/search?q=Apple%20AirPods%202&_lc=Vk4wMzQwMjcwMDM=&headphone_type=108271"
55

66
req = requests.get(URL)
7-
file = open("template.html", "w+")
8-
file.writelines(req.text)
9-
file.close()
10-
soup = BeautifulSoup(req.text, "lxml")
7+
8+
soup = BeautifulSoup(req.text, "lxml")
9+
items = soup.findAll("a", {"class": "search-a-product-item"})
10+
#body > div.wrap > div > div.product-listing > div:nth-child(2) > div.product-box-list > div:nth-child(2)
11+
print(len(items))
12+
13+
for item in items:
14+
15+
#https://tiki.vn
16+
url = "https://tiki.vn" + item.get("href")
17+
18+
# print(item.get("title"))
19+
price = item.find("span", {"class":"final-price"}).get_text()
20+
21+
22+
# <span class="price-regular">6.900.000đ</span>
23+
regularPrice = item.find("span", {"class":"price-regular"}).get_text()
24+
25+
finalPrice = str(price).split("đ")
26+
print(finalPrice[0].strip() + " (" +finalPrice[1].strip()+")" + " | " + regularPrice)
27+
28+
29+
30+
print(url)
31+
32+
33+
# print(items[0])

FunPy02_SanDealTiki/item.html

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
<div data-seller-product-id="13715113" product-sku="3818146479362" data-title="Tai Nghe Bluetooth Apple AirPods 2 True Wireless - MV7N2 - Nhập Khẩu Chính Hãng - Hộp Sạc Thường" data-price="3589000" data-id="46623654" data-score="" data-brand="Apple" data-category="Thiết Bị Số - Phụ Kiện Số/Thiết Bị Âm Thanh và Phụ Kiện/Tai Nghe Bluetooth/Tai Nghe True Wireless" rel="" class="product-item search-div-product-item ">
2+
<a data-id="46623654" href="/tai-nghe-bluetooth-apple-airpods-2-true-wireless-mv7n2-nhap-khau-chinh-hang-hop-sac-thuong-p46623654.html?src=search&amp;2hi=0&amp;keyword=Apple+AirPods+2" title="Tai Nghe Bluetooth Apple AirPods 2 True Wireless - MV7N2 - Nhập Khẩu Chính Hãng - Hộp Sạc Thường" class="search-a-product-item" data-version="v1">
3+
<div class="content ">
4+
<span class="image">
5+
<img class="product-image img-responsive" src="https://salt.tikicdn.com/cache/280x280/ts/product/d8/9f/ed/42f4410cad6c8c13eb1cdd95080f971e.jpg" alt="">
6+
7+
<div class="badge-top" style="
8+
position: absolute;
9+
z-index: 2;
10+
top: 0;
11+
left: 0;
12+
">
13+
<div class="item" style="
14+
padding: 2px 2px 2px 0;
15+
display: flex;
16+
z-index: 2;
17+
background: #009900;
18+
">
19+
<div style="
20+
position: absolute;
21+
right: -5px;
22+
top: 0;
23+
width: 100%;
24+
height: 100%;
25+
background: #009900;
26+
transform: skew(-25deg);
27+
z-index: -1;
28+
"></div>
29+
30+
<img style="
31+
margin: 0 0 0 5px;
32+
width:16px;
33+
height:16px;
34+
" src="https://salt.tikicdn.com/ts/upload/f3/74/46/f4c52053d220e94a047410420eaf9faf.png">
35+
36+
<p class="service-text" style="
37+
font-size: 11px;
38+
line-height: 16px;
39+
padding: 1px 0 0 5px;
40+
color:#ffffff;
41+
"> Freeship </p>
42+
</div>
43+
</div>
44+
45+
<span class="product-right-icon">
46+
</span>
47+
48+
49+
50+
51+
52+
</span>
53+
54+
55+
<p class="icons">
56+
</p>
57+
58+
59+
<div class="badge-service">
60+
<div class="item" style="
61+
background: ">
62+
<img style="
63+
width:56px;
64+
height:16px;
65+
" src="https://salt.tikicdn.com/ts/upload/9f/32/dd/8a8d39d4453399569dfb3e80fe01de75.png">
66+
67+
<p class="service-text" style="
68+
color:#009900;
69+
"> </p>
70+
</div>
71+
</div>
72+
73+
74+
<p class="title">
75+
76+
77+
Tai Nghe Bluetooth Apple AirPods 2 True... </p>
78+
79+
80+
<p class="price-sale">
81+
<span class="final-price">
82+
3.589.000đ <span class="sale-tag sale-tag-square">-48%</span>
83+
</span>
84+
<span class="price-regular">6.900.000đ</span>
85+
<span class="sale-tag sale-tag-square">-48%</span>
86+
</p>
87+
<div class="badge-under_price">
88+
<img style="
89+
width:124px;
90+
height:18px;
91+
" src="https://salt.tikicdn.com/ts/upload/51/ac/cc/528e80fe3f464f910174e2fdf8887b6f.png">
92+
</div>
93+
</div>
94+
95+
<div class="price-tag-wrap">
96+
</div>
97+
98+
99+
<div class="review-wrap">
100+
<p class="rating">
101+
<span class="rating-content">
102+
<i class="star"></i>
103+
<i class="star"></i>
104+
<i class="star"></i>
105+
<i class="star"></i>
106+
<i class="star"></i>
107+
<span style="width:88%">
108+
<i class="star"></i>
109+
<i class="star"></i>
110+
<i class="star"></i>
111+
<i class="star"></i>
112+
<i class="star"></i>
113+
</span>
114+
</span>
115+
</p>
116+
<p class="review">(920 nhận xét)</p>
117+
</div>
118+
119+
120+
121+
<div class="installment-wrapper">
122+
<p class="installment">
123+
<span class="installment-price-v2">Trả góp 0%</span>
124+
</p>
125+
</div>
126+
127+
128+
<div class="ship-label-wrapper">
129+
130+
131+
132+
</div>
133+
134+
135+
136+
137+
</a>
138+
</div>

0 commit comments

Comments
 (0)