Skip to content

Instantly share code, notes, and snippets.

@theagoliveira
Created October 17, 2019 03:03
Show Gist options
  • Save theagoliveira/757f92ea80c0be3697f9d252e3c1e4ee to your computer and use it in GitHub Desktop.
Save theagoliveira/757f92ea80c0be3697f9d252e3c1e4ee to your computer and use it in GitHub Desktop.
Get the price of the first result from a MercadoLivre website search
#!/usr/bin/env python
"""
Get the price of the first result from a MercadoLivre website search.
"""
import argparse
import re
import urllib.request as req
__author__ = 'Thiago Cavalcante'
DESC = 'Get the price of the first result from a MercadoLivre website search.'
TAG = ('<span class="price__fraction">([0-9.]+)</span> ' +
'<span class="price__decimals">([0-9]+)</span>' +
'|<span class="price__fraction">([0-9.]+)</span></div>')
parser = argparse.ArgumentParser(description=DESC)
parser.add_argument('-t', '--term', help='Search term', required=True)
args = parser.parse_args()
url = 'https://lista.mercadolivre.com.br/' + args.term.lower().replace(' ', '-')
src = str(req.urlopen(url).read())
prices = re.findall(TAG, src)
price = list(filter(None, list(prices[0])))
ending = ''
if len(price) == 1:
ending = ',00'
price = ','.join(price) + ending
print('URL: ' + url)
print('Price: R$ ' + price)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment