def __scrape_coupon(self): try: coupon_link = WebDriverWait(self.driver, 1).until(EC.visibility_of_element_located((By.ID, 'applicable_promotion_list_sec'))) ActionChains(self.driver).move_to_element(coupon_link).perform() coupon_text = self.driver.find_element_by_id('a-popover-content-3').text pattern = r'Ahorra un (?P<discount>\d+([\,\.]\d+)?)%.*c.digo\s(?P<code>\S+).*' match = re.search(pattern, coupon_text) if match is not None: self.coupon_code = match.group('code') self.coupon_discount = match.group('discount') self.coupon_price = float(money_parser.price_dec(self.price)) * float(self.coupon_discount) / 100 self.coupon_price = f'{round(self.coupon_price, 2)} €' except (NoSuchElementException, TimeoutException): print('[Scraper Info] No Coupon in --> ' + self.url) try: coupon_text = self.driver.find_element_by_id('applicable_promotion_list_sec').text pattern = r'\s+(?P<discount>(\d+)[\.\,]?(\d+)?%) OFF: (?P<code>\w+)\.\s+' match = re.search(pattern, coupon_text) if match is not None: self.coupon_code = match.group('code') self.coupon_discount = match.group('discount') self.coupon_price = float(money_parser.price_dec(self.price)) * float(self.coupon_discount) / 100 self.coupon_price = f'{round(self.coupon_price, 2)} €' except (NoSuchElementException, TimeoutException): print('[Scraper Info] No Coupon in --> ' + self.url) except Exception as e: print('[ERROR Scraper Info] Exception is not being well handled in coupon scraper--> \n' + str(e))
def _clean_size(size): # Use Price normalizer try: size = int(price_dec(str(size))) except Exception as e: print(e, size) return None if size_is_in_range(size): return size else: return None
def price_normalize(price_str): # try initial default (roubust) parse try: price = price_dec(price_str) except Exception as e: print(e) return 0.0 if price_is_in_range(price): return float(price) else: return 0.0
def parse_item(item): category = get_category_object(item) description = item['description'] name = item['title'] try: price = Money(price_dec(str(item['price'])), settings.DEFAULT_CURRENCY) except Exception: if 'price' in item: logger.error('Failed to parse price %s', item['price'], exc_info=True) else: logger.error('Price parsing error', exc_info=True) raise result = { 'category_id': category.pk, 'price': price, 'name': name, 'description': description, 'seo_description': description[:300], 'seo_title': name[:70] } return result
def test_price_dec_error(): with pytest.raises(ValueError): price_dec('') with pytest.raises(ValueError): price_dec('7 | 128')
def test_price_dec_default(): assert Decimal('0') == price_dec('', default=Decimal('0')) assert 0 == price_dec('1..10', default=0) assert price_dec('410.5 - 555', default=None) is None
def test_price_dec_value(): assert Decimal('1') == price_dec('+1') assert Decimal('-10.99') == price_dec(': -10.99$')
def parse_currency(s): return float(money_parser.price_dec(s))
driver.get(itemURL) # After signing in go to items page for watching # # second check price # appropriatePriceFound = False price = 0 priceIDNames = ["priceblock_ourprice", "price_inside_buybox"] searchIndex = 0 print("Checking price") while not appropriatePriceFound: priceText = driver.find_elements_by_id(priceIDNames[searchIndex]) if len(priceText) > 0: appropriatePriceFound = True price = price_dec( priceText[0].text) # parse the price string and convert to decimal if price < priceLimit: print(f"Price is ${price}, which is appropriate") else: print(f"${price} is too expensive for the limit of ${priceLimit}") if refresh_if_too_expensive: driver.refresh() print("Refreshing") appropriatePriceFound = False else: exit(-1) elif searchIndex < len(priceIDNames) - 1: #print(f"{priceIDNames[searchIndex]} not found. Trying next") # Debug searchIndex += 1 else: #print(f"{priceIDNames[searchIndex]} not found.") # Debug