Skip to content

Instantly share code, notes, and snippets.

@dannguyen
Last active June 13, 2022 20:04
Show Gist options
  • Select an option

  • Save dannguyen/57423dbcb1713d31b659 to your computer and use it in GitHub Desktop.

Select an option

Save dannguyen/57423dbcb1713d31b659 to your computer and use it in GitHub Desktop.
A Bash script, using the jq JSON-parser, to scrape all the NHTSA 5 star vehcile ratings from its API http://www.nhtsa.gov/webapi/Default.aspx?SafetyRatings/API/5
years=$(curl -s 'http://www.nhtsa.gov/webapi/api/SafetyRatings?format=json' | jq -r '.Results[] .ModelYear')
for year in $years; do
echo "$year"
echo "######"
curl -s "http://www.nhtsa.gov/webapi/api/SafetyRatings/modelyear/$year?format=json" | jq -r '.Results[] .Make' | sed 's/ /%20/' | while read -r carmake; do
# Get the year and make
echo " $carmake"
echo " ======="
curl -s "http://www.nhtsa.gov/webapi/api/SafetyRatings/modelyear/$year/make/$carmake?format=json" | jq -r '.Results[] .Model' | sed 's/ /%20/' | while read -r model; do
echo " $model"
echo " -------"
# Get the year, make, and model
curl -s "http://www.nhtsa.gov/webapi/api/SafetyRatings/modelyear/$year/make/$carmake/model/$model?format=json" | jq -r '.Results[] .VehicleId' | while read -r id; do
echo " $id: $year - $carmake - $model"
curl -s "http://www.nhtsa.gov/webapi/api/SafetyRatings/VehicleId/$id?format=csv" -o "$id.csv"
done
done
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment