Created
March 23, 2018 14:09
-
-
Save cubedtear/e825f5eb9f77f18e40756ad8a1a14e4d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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