-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.py
111 lines (96 loc) · 3.93 KB
/
server.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
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
101
102
103
104
105
106
107
108
109
110
111
from bs4 import BeautifulSoup
from requests import HTTPError, ConnectionError
import requests
from datetime import datetime
def default_error_message():
return {
"Message": "The product you are trying to search is not available, or doesn't exist, or was not found. Try again."
}
def http_error_message(error):
return {
"Message": f"A http_error(error: {error}) ocurred, try again."
}
def connection_error_message():
return {
"Message": "A connection_error ocurred, try again."
}
def find_all_products_data(product_name: str):
if product_name == '':
return default_error_message()
try:
product_name_to_use = product_name.replace(' ', '-')
url = f'https://lista.mercadolivre.com.br/{product_name_to_use}'
website = requests.get(url).text
soup = BeautifulSoup(website, 'lxml')
#print(soup.prettify())
response = {
"products": [],
"searched_day": datetime.today().strftime('%A, %B %d, %Y %H:%M:%S')
}
products_cards = soup.find_all('div', class_='andes-card andes-card--flat andes-card--default ui-search-result ui-search-result--core andes-card--padding-default')
product_id = 0
for product_card in products_cards:
title = product_card.find('h2', class_='ui-search-item__title').text
product_link = product_card.find('a', class_='ui-search-item__group__element ui-search-link')
price1 = product_card.find('span', class_='price-tag-fraction').text
cents = product_card.find('span', class_='price-tag-cents')
if cents:
cents2 = str(cents)
coin = product_card.find('span', class_='price-tag-symbol').text
price = f'{price1}.{cents2[30:32]}'
else:
price = price1
product = {
"id": product_id,
"name": title,
"link": product_link['href'],
"price": price,
"coin": coin
}
response['products'].append(product)
product_id += 1
return response
except ConnectionError:
return connection_error_message()
except HTTPError as err:
return http_error_message(err)
except:
return default_error_message()
def find_product_data(product_name: str):
if product_name == '':
return default_error_message()
try:
product_name_to_use = product_name.replace(' ', '-')
url = f'https://lista.mercadolivre.com.br/{product_name_to_use}'
website = requests.get(url).text
soup = BeautifulSoup(website, 'lxml')
product_card = soup.find('div', class_='andes-card andes-card--flat andes-card--default ui-search-result ui-search-result--core andes-card--padding-default')
title = product_card.find('h2', class_='ui-search-item__title').text
product_link = product_card.find('a', class_='ui-search-item__group__element ui-search-link')
price1 = product_card.find('span', class_='price-tag-fraction').text
cents = product_card.find('span', class_='price-tag-cents')
cents2 = str(cents)
coin = product_card.find('span', class_='price-tag-symbol').text
price = f'{price1}.{cents2[30:32]}'
product = {
"name": title,
"link": product_link['href'],
"price": price,
"coin": coin
}
response = {
"product": [],
"searched_day": datetime.today().strftime('%A, %B %d, %Y %H:%M:%S')
}
response['product'].append(product)
return response
except ConnectionError:
return connection_error_message()
except HTTPError as err:
return http_error_message(err)
except:
return default_error_message()
if __name__ == "__main__":
print(find_all_products_data('tesoura'))
#print('\n\n\n\n')
#print(find_product_data('caneta'))