Skip to content

Instantly share code, notes, and snippets.

@AskAlice
Created September 1, 2021 23:35
Show Gist options
  • Save AskAlice/c6d0f82eae6feed75c0a1e1c7f31fb6e to your computer and use it in GitHub Desktop.
Save AskAlice/c6d0f82eae6feed75c0a1e1c7f31fb6e to your computer and use it in GitHub Desktop.
USPS informed delivery scraping
const packages = [];
document.querySelectorAll("div.pack_row").forEach(package => {
let pack = {}
const details = package.querySelector(".pack_details #coltextR2");
const greenText = details.querySelector("span:nth-child(5)")?.innerText;
pack.provider = greenText?.startsWith("Delivered at") ? undefined : greenText;
pack.trackingNumber = details.querySelector(".pack_h4").innerText.replace("/\s/g", "");
const statusArray = package.querySelector(".pack_coltext").innerText.trim().replace(/[\s\t]{2,}/g,"~~").split("~~");
const statusKeys = ["status","longStatus","date"];
pack = {...pack, ...Object.fromEntries(statusKeys.map((_, i) => [statusKeys[i], statusArray[i]]))}
packages.push(pack);
});
console.log(packages);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment