Created
October 4, 2022 08:25
-
-
Save RedHatter/f21efbc60cf173799086227baae59fdc to your computer and use it in GitHub Desktop.
Scrap amazon order history and download invoices as PDFs
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
import puppeteer from "puppeteer"; | |
function clickLink(page, selector) { | |
return Promise.all([page.click(selector), page.waitForNavigation()]); | |
} | |
const USERNAME = "[email protected]"; | |
const PASSWORD = "************"; | |
const PAGES = 2; | |
const browser = await puppeteer.launch({ headless: false }); | |
const page = await browser.newPage(); | |
await page.goto("https://www.amazon.com/gp/css/order-history"); | |
await page.type("input#ap_email", USERNAME); | |
await clickLink(page, "input#continue"); | |
await page.type("input#ap_password", PASSWORD); | |
await clickLink(page, "input#signInSubmit"); | |
let i = 0; | |
do { | |
i++; | |
const links = await page.$$eval( | |
'a[href*="/gp/css/summary/print.html?orderID"]', | |
(links) => links.map((o) => o.getAttribute("href")) | |
); | |
await Promise.all( | |
links.map(async (link) => { | |
const page = await browser.newPage(); | |
await page.goto(`http://www.amazon.com${link}`); | |
const orderIdStartIndex = link.indexOf("orderID=") + "orderID=".length; | |
const orderId = link.substring( | |
orderIdStartIndex, | |
link.indexOf("&", orderIdStartIndex) | |
); | |
console.log(orderId); | |
// const content = await page.content(); | |
// let priceStartIndex = content.indexOf("Grand Total:"); | |
// priceStartIndex = content.indexOf("<b>", priceStartIndex) + "<b>".length; | |
// const price = content.substring( | |
// priceStartIndex, | |
// content.indexOf("</b>", priceStartIndex) | |
// ); | |
// console.log(orderId, price); | |
await page.pdf({ | |
// path: `invoices/${orderId}_${price}.pdf`, | |
path: `invoices/${orderId}.pdf`, | |
}); | |
await page.close(); | |
}) | |
); | |
await clickLink(page, ".a-last"); | |
} while (i < PAGES); | |
await page.close(); | |
await browser.close(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment