Skip to content

Instantly share code, notes, and snippets.

@wintrdotcom
Last active September 7, 2020 12:35
Show Gist options
  • Save wintrdotcom/d65f342dd1fe00432ed9f7865824715d to your computer and use it in GitHub Desktop.
Save wintrdotcom/d65f342dd1fe00432ed9f7865824715d to your computer and use it in GitHub Desktop.
Scrape Aliexpress product list, get a free WINTR API key on https://www.wintr.com
const WINTR_API_KEY = "your_wintr_api_key"
const TARGET_URL = "https://www.aliexpress.com/wholesale?SearchText=phone"
const axios = require("axios")
const options = {
method: "post",
responseType: "json",
data: {
"apikey": WINTR_API_KEY,
"url": TARGET_URL,
"jsrender": true, // comment this to run the query faster (it might fail more often tho)
"renderuntil": "domloaded", // comment this to run the query faster (it might fail more often tho)
"headers": {
"Accept-Language": "en-US,en;q=0.9,es;q=0.8",
"Referer": "https://www.google.com"
},
"outputschema": {
"list": {
"group": "body",
"data": {
"scripts": {
"selector": "script[type=\"text/javascript\"]",
"attr": "*html*"
}
}
}
}
},
url: "https://api.wintr.com/fetch"
}
axios(options)
.then((result) => {
let products = []
if (result.data.content.list && result.data.content.list.length) {
result.data.content.list[0].scripts.forEach((script) => {
if (script.includes("\"resultCount\"")) {
try {
products = JSON.parse(
script.split("window.runParams = {};").join("")
.split("window.runParams =").join("")
.split("\"resultType\":\"normal_result\"};")[0]
.trim() + "\"resultType\":\"normal_result\"}"
).items
} catch(e) {
console.error("ERROR: Cannot parse products JSON from page scripts")
}
}
})
}
console.log(products)
})
.catch((err) => {
console.error(err)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment