Created
November 20, 2023 13:09
-
-
Save nemo0/728dc83389419817708866c4cafcb336 to your computer and use it in GitHub Desktop.
Node.js code to help you get started with the Nimble E-commerce API for scraping Walmart data
This file contains 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
require("dotenv").config(); | |
const axios = require("axios"); | |
const fs = require("fs"); | |
const { AsyncParser } = require("@json2csv/node"); | |
const token = Buffer.from( | |
process.env.NIMBLE_USERNAME + ":" + process.env.NIMBLE_PASSWORD | |
).toString("base64"); | |
async function scrapeWalmartData(credentials, data) { | |
const url = "https://api.webit.live/api/v1/realtime/ecommerce"; | |
try { | |
const response = await axios.post(url, data, { | |
headers: { | |
Authorization: `Basic ${credentials}`, | |
"Content-Type": "application/json", | |
}, | |
}); | |
return response.data; | |
} catch (error) { | |
throw error; | |
} | |
} | |
const requestData = { | |
parse: true, | |
vendor: "walmart", | |
url: "https://www.walmart.com/search?q=hair%20dryer&typeahead=hair%20dry", | |
format: "json", | |
render: true, | |
country: "ALL", | |
locale: "en", | |
}; | |
(async () => { | |
try { | |
const data = await scrapeWalmartData(token, requestData); | |
const fields = ["id", "name", "image", "price", "averageRating"]; | |
const parser = new AsyncParser({ fields }); | |
const csv = await parser | |
.parse(data.parsing.entities.SearchResult) | |
.promise(); | |
fs.writeFile("output.csv", csv, function (err) { | |
if (err) throw err; | |
console.log("CSV file successfully saved."); | |
}); | |
} catch (error) { | |
console.error("Error:", error); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment