Scrapping Prices from Amazon with Napkin.io for free...
Tags: webscrapping
Published: 2023-08-14 07:03:43

First of all, what is [Napkin](https://www.napkin.io/) ?


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eb782bktvohomcv5rqeu.png)

Here is the function I created in Napkin in order to query Amazon. With that in place we can avoid the majorities of CORS problems.

Notice that, in the following script, the requests can only be originated from "flems.io".

```js
/**
* @param {NapkinRequest} req
* @param {NapkinResponse} res
*/

const Origin = 'https://r.flems.io'

const axios = require("axios")

async function fetchData(url) {
  const resp = await axios.get(url)
  const data = await resp.data
  return data;
}

export default async (req, res) => {
  if (req.headers.Origin != Origin) {
    res.json({
      "error": req.headers.Origin != Origin && "NOT ALLOWED"
    })
  }

  else {
    var info = await fetchData(req.query.url)
    res.json({
      "message": info,
    })
  }
}
```
So we will need to enter the ASIN of the product :

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d4kmj4cn1r2uf3obecxs.png)

Here is the code I use, you will have to adapt it for your needs.
It's just a proof of concept.

```js
const url = (ref) =>
  `https://salve-didio.npkn.net/cross/?url=https://www.amazon.com.be/dp/${ref}`

const parser = new DOMParser()

const rgpd = "#sp-cc";
const bybox = "#buybox";
const whole_price = ".a-price-whole";
const price_fraction = ".a-price-fraction";
const price_symbol = ".a-price-symbol";
const product_title = "#productTitle";
const btn_display_page = document.getElementById("display_page")
const loader = document.getElementById("loader");

function fetch_data(ref) {
  fetch(url(ref))
    .then(response => response.json())
    .then(m => show_data(m.message))
}

// if any
function hide_rgpd(doc) {
  const _rgpd = doc.querySelector(rgpd)
  _rgpd.style.display = "none"
}

function get_price(doc) {
  const bb = doc.querySelector(bybox);
  const price = bb.querySelector(whole_price)
  const price_frac = bb.querySelector(price_fraction)
  const price_symb = bb.querySelector(price_symbol);
  const title = doc.querySelector(product_title)
  const price_text = `
    <h2>${title.innerText}</h2>
    <span class="pricetext">price : ${price.innerText}${price_frac.innerText}${price_symb.innerText}</span>`
  result.innerHTML = price_text
}

function show_page(doc) {
  target.style.display = "block"
  target.srcdoc = doc.innerHTML
}

function show_data(page) {
  const _doc = parser.parseFromString(page, 'text/html')
  const doc = _doc.documentElement
  hide_rgpd(doc)
  get_price(doc)
  btn_display_page.style.display = "block"
  loader.style.display = "none";
  btn_display_page.onclick = () => show_page(doc)
}


function init_page() {
  display_page.style.display = "none"
  target.style.display = "none";
  result.innerText = "";
  loader.style.display = "block";
}

function fetch_price() {
  init_page();
  fetch_data(dp.value || "B08GYSTR27")
}
```
You can play with it here [DEMO](https://flems.io/#0=N4IgtglgJlA2CmIBcAWAzAOgOwBoQGcBjAJwHtZZkBtABhxoF08AzCBfa0AOwEMxEkIDAAsALmEp5CpLqPizkIADxQIANwAEhWD3z4AvAB0Q02Twhd4xYwD5DXDRqXCAjHYeONAZXg9ihYQ0AQTAeAC8ZDQAHYghCeHwNACMAT2CvAEkAOXtHJQB6V3c81U1tXQNjZlJiMFt07I0kJwsogFdRDWgjECgo42ideOFyKCsegCEaAA4AcQBNLwAVACUAJiwB-OLPJSSO0UiZbTiAax7meFEAgH0YuPgACgBKetmAUSWNAAUVjIBhd5OfL7USHLg7PKg8EafCiFIIHqqfBRHQpJBcGTwAbdYzI1E8FJ3HgAc2xIBsGgAIhkvN8ADJBeY-IIfYHQmQ7AqlLnCYgaba5JylLpQHrEBJtWCiWzc9RciDMYh8eCinqiPxkmUgWHwxF4iAotEYrGy-KK5X8Lki3EgWCkHhjawUjT0gDyQRpWVmGF9GGBPPscrUNhAeHw8AQhFEEBkHEEAE4kGsUCAAL44bgqxQYABWHCkMjkCkEpjhGjaxFgGn0GkeEuYzxrOwABmJRFF8Eh8vl8DxYGp4ABaVSqUgYLhRU5cCdXfIkUh6fIAfkrsH07c73fyAHc9xg+OEZBhpGAMEl4Pk+vkACTABtplv2exlzpRPwR-m1yw76lugCy3wflYLzPlwr4aMQJJRFANYaMYADEKJDoQhDGAA3C+cadKkSSkAAHnBiH7CkeH4RhWFcOWO4jAgdyxPERFCDwQ73PEQ40eQ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