コード例 #1
0
    async def _setProductPrice(self, soup: BeautifulSoup,
                               product: Product) -> ProductChanged:
        isProductChanged = False

        try:
            thisProductInfo = soup.find("div",
                                        class_="b-pdp-product-info-section")
            priceElem = thisProductInfo.find(
                "span", class_="b-product-tile-price-item")
            priceAndCurrencyStr = priceElem.text.strip()  # like '98,55 €'
            # Split price and currency
            match = re.search(r"(([0-9.,]+)\s+([^0-9]+))", priceAndCurrencyStr)
            priceStr, currencyStr = match.group(2), match.group(3)
            priceFloat = float(priceStr.replace(",", "."))

        except Exception as e:
            logger.warning("Failed finding product price or currency. %s. %s",
                           e, product.url)
            self._failCount += 1

        else:
            logger.debug(
                "Extracted product price & currency from HTML code. %s",
                product.url)
            if not product.basePrice or product.basePrice != priceFloat:
                product.basePrice = priceFloat
                product.currency = currencyStr
                isProductChanged = True  # note that we never switch back to False

        finally:
            return isProductChanged
コード例 #2
0
    async def _setProductPrice(self, soup: BeautifulSoup,
                               product: Product) -> ProductChanged:
        """ We need to find & extract price and currency from the following JS code:
        <script>
            fbq('track', 'ViewContent', {
            value: '64',
            currency: 'EUR',
            content_ids: '245134',
            content_type: 'product_group',
            });
        </script>
        """
        isProductChanged = False

        jsCodeToFind = "fbq\\(\'track\', \'AddToCart\'"
        reCompiled = re.compile(jsCodeToFind)

        try:
            javascriptStr = soup.find("script",
                                      attrs={
                                          "type": "text/javascript"
                                      },
                                      text=reCompiled).string

            priceStr = re.search(r"value:\s+(.*?)[,\n]",
                                 javascriptStr).group(1).strip("\'")
            priceFloat = float(priceStr)
            currency = re.search(r"currency:\s+(.*?)[,\n]",
                                 javascriptStr).group(1).strip("\'")

        except Exception as e:
            logger.warning("Failed finding product price or currency. %s. %s",
                           e, product.url)
            self._failCount += 1

        else:
            logger.debug("Extracted product price & currency from JS code. %s",
                         product.url)
            if not product.basePrice or product.basePrice != priceFloat:
                product.basePrice = priceFloat
                product.currency = currency
                isProductChanged = True  # note that we never switch back to False

        finally:
            return isProductChanged
コード例 #3
0
    async def _setProductPrice(self, soup: BeautifulSoup, product: Product) -> ProductChanged:
        isProductChanged = False

        try:
            elem = soup.find("div", class_="buybox").find("div", class_="price")
            priceStr = elem.find("meta", {"itemprop": "price"})["content"]
            currencyStr = elem.find("meta", {"itemprop": "priceCurrency"})["content"]
            priceFloat = float(priceStr.replace(",", "."))

        except Exception as e:
            logger.warning("Failed finding product price or currency. %s. %s", e, product.url)
            self._failCount += 1

        else:
            logger.debug("Extracted product price & currency from HTML code. %s", product.url)
            if not product.basePrice or product.basePrice != priceFloat:
                product.basePrice = priceFloat
                product.currency = currencyStr
                isProductChanged = True  # note that we never switch back to False

        finally:
            return isProductChanged