Skip to content

Instantly share code, notes, and snippets.

@cubedtear
Created March 23, 2018 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cubedtear/e825f5eb9f77f18e40756ad8a1a14e4d to your computer and use it in GitHub Desktop.
Save cubedtear/e825f5eb9f77f18e40756ad8a1a14e4d to your computer and use it in GitHub Desktop.
import requests
from lxml import html, etree
item_prices = []
for page in range(1, 10):
r = requests.get('https://www.chollometro.com/nuevos', {'page': str(page), 'ajax': 'true'})
res = html.fromstring(r.text)
items = res.xpath('//div[contains(@class, "threadCardLayout--card")]')
for item in items:
t = etree.tostring(item)
if item.xpath('.//article[contains(@class, "thread--expired")]'):
continue
price_elems = item.xpath('.//span[contains(@class, "thread-price")]')
if not price_elems:
continue
title_elem = item.xpath('.//strong[contains(@class, "thread-title")]/span/a')
title = title_elem[0].text.strip()
price = price_elems[0].text.replace("€", "").replace(",", ".")
try:
item_prices.append((title, float(price), title_elem[0].attrib['href']))
except ValueError:
pass
item_prices.sort(key=lambda x: x[1])
longest = len(max(item_prices, key=lambda x: len(x[0]))[0])
for title, price, url in item_prices:
print("{price:7} - {title:{width}s} - {url}".format(title=title, price=price, url=url, width=longest))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment