Skip to content

Instantly share code, notes, and snippets.

@timmyha
Last active April 4, 2024 20:32
Show Gist options
  • Select an option

  • Save timmyha/a29ba88c1653407f4f78829f425e095a to your computer and use it in GitHub Desktop.

Select an option

Save timmyha/a29ba88c1653407f4f78829f425e095a to your computer and use it in GitHub Desktop.
shopify product scraper
// run in console of a shopify store /products.json endpoint.
let json = JSON.parse(document.querySelector("pre").innerHTML);
const parseJSON = (obj) => {
let csvArr = [['title','description','sku','image_originator_url']];
for (product of obj.products) {
const randomSKU = Math.floor(Math.random() * 9999999999);
const row = [
product.title,
product.title,
randomSKU,
product.images[0] ? product.images[0].src : null,
];
csvArr.push(row)
}
return csvArr
}
const stringifyCSV = (arr) => {
let data = "";
const tableData = [];
for (const row of arr) {
const rowData = [];
for (const cell of row) {
rowData.push(cell);
}
tableData.push(rowData.join(","));
}
return data += tableData.join("\n");
}
const createCSV = (json) => {
const obj = parseJSON(json)
const data = stringifyCSV(obj)
const a = document.createElement("a");
a.href = URL.createObjectURL(new Blob([data], { type: "text/csv" }));
a.setAttribute("download", "data.csv");
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
}
createCSV(json);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment