-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.py
75 lines (66 loc) · 2.84 KB
/
script.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
import json
import sys
from time import sleep
from amazon_scraper import AmazonScraper
HEADERS = {
'authority': 'www.amazon.com.mx',
'pragma': 'no-cache',
'cache-control': 'no-cache',
'dnt': '1',
'upgrade-insecure-requests': '1',
'User-Agent': 'Safari/537.36',
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'sec-fetch-site': 'none',
'sec-fetch-mode': 'navigate',
'sec-fetch-dest': 'document',
'accept-language': 'en-GB,en-US,es,es-MX;q=0.9,en;q=0.8',
}
AMAZON_URL='https://www.amazon.com.mx'
AMAZON_SEARCH_URL='https://www.amazon.com.mx/s?k='
def main():
validate_args()
scrapper = AmazonScraper('products.yml')
print('===============================INICIO===============================')
process_search(scrapper)
print('===============================FIN===============================')
def process_search(scrapper):
search = sys.argv[1]
discount = int(sys.argv[2])
print(f'Buscando {search} con descuento de {discount}% ...')
retries = 0
url = AMAZON_SEARCH_URL + search.replace(' ','+')
print(url)
while retries < 5:
data = scrapper.scrape(url, HEADERS)
retries = examine_data(data, retries, discount)
if (retries == 5):
print(f'No se encontraron datos para la busqueda {search} después de {retries} reintentos')
def validate_args():
if (len(sys.argv) == 0):
raise Exception('No se proporcionó el producto a buscar')
if (len(sys.argv) is not 3):
raise Exception('Argumentos incorrectos: debe de ser [PRODUCTO] [DESCUENTO]. Ejemplo: "libro python" 20')
def print_product(product, price, regular, discount, percentage):
print('-------------------------------------------------------------------')
print(f'Producto: {product["name"]}')
print(f'Enlace: {AMAZON_URL + product["link"]}')
print(f'Precio actual: {price}')
print(f'Precio regular: {regular}')
print(f'Descuento: ${discount:.0f} ({percentage:.0f}%)')
def examine_data(data, retries, discount_search):
if data and data['products']:
retries = 999
for product in data['products']:
if (product['regular-price'] and product['price']):
regular = float(str(product['regular-price']).replace('$', '').replace(',', ''))
price = float(str(product['price']).replace('$', '').replace(',', ''))
discount = regular - price
percentage = discount * 100 / regular
if (percentage >= discount_search):
print_product(product, price, regular, discount, percentage)
else:
retries += 1
sleep(5)
return retries
if __name__ == '__main__':
main()