Skip to content

Instantly share code, notes, and snippets.

@BitesizedLion
Created August 17, 2024 19:34
Show Gist options
  • Save BitesizedLion/2223f0bc25153d1038e84d3ba6890f52 to your computer and use it in GitHub Desktop.
Save BitesizedLion/2223f0bc25153d1038e84d3ba6890f52 to your computer and use it in GitHub Desktop.
Scrape Tunabyggen's available garages and output to CSV. Includes ID, Display Name, Description, Price and Complete Address.
import requests
import csv
headers = {
'Accept': 'application/json, text/plain, */*',
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36',
'X-Api-Key': 'pJnKrR6B3FzRNFsF33xL8LhSs55KPJrm',
'X-Momentum-Client': 'momentum.se-fastighetminasidor',
'X-Momentum-Client-Id': 'EAP-47356',
'X-Momentum-Client-Version': '5.20.3.15457',
'X-Momentum-Device-Key': 'e8q66tgd9jsqalnk9alijf',
}
url = "https://tunabyggen-fastighet.momentum.se/Prod/Tunabyggen/PmApi/v2/market/objects?type=parking&limit=500"
response = requests.get(url, headers=headers)
data = response.json()
csv_filename = "garages.csv"
csv_columns = ["id", "displayName", "description", "price", "completeAddress"]
with open(csv_filename, 'w', newline='', encoding='utf-8') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=csv_columns)
writer.writeheader()
for item in data["items"]:
if item.get("thumbnail", {}).get("exists") and item["thumbnail"]["displayName"] == "Garage bild":
garage_id = item["id"]
display_name = item["displayName"]
description = item["description"]
price = item["pricing"]["price"]
detail_url = f"https://tunabyggen-fastighet.momentum.se/Prod/Tunabyggen/PmApi/v2/market/objects/{garage_id}"
detail_response = requests.get(detail_url, headers=headers)
detail_data = detail_response.json()
complete_address = detail_data.get("location", {}).get("address", {}).get("completeAdress", "")
print(complete_address)
writer.writerow({
"id": garage_id,
"displayName": display_name,
"description": description,
"price": price,
"completeAddress": complete_address
})
print(f"Data has been written to {csv_filename}")
id displayName description price completeAddress
mG8T8dvt9KYKjKj6gvmbRWVY Stjärngatan 2 Garageplats med el 357.0 Stjärngatan 2, 78453, Borlänge
PgbmMJKxgX7ymtDFfKtDDR6X Kometgatan 6 Garageplats med el 357.0 Kometgatan 6, 78453, Borlänge
cMpChVGQ9fmY4KKM6gpxYW44 Stjärngatan 7 Garageplats med el 357.0 Stjärngatan 7, 78453, Borlänge
k8gJtPkyYb7BdFHKbWKbw8fW Norrskensgatan 8 Garageplats med el 357.0 Norrskensgatan 8, 78453, Borlänge
GXkmt88rXxBPfMwWftKfPXdB Tunagatan 13 Plats i varmgarage. 425.0 Tunagatan 13, 78434, Borlänge
dCbbtGrXxpPBJW4PxJWtQTk4 Stjärngatan 17 Garageplats med el 357.0 Stjärngatan 17, 78453, Borlänge
Gbtm4H9BBvc64D9v69XVX9dr Planetgatan 6 Garageplats med el 357.0 Planetgatan 6, 78453, Borlänge
gFC4CGJxbgTmJpjPtBtrK4xF Tunagatan 13 Plats i varmgarage. 425.0 Tunagatan 13, 78434, Borlänge
tPqgHhVXBvMmDPHryHTM8M9d Bondegatan 8 Garageplats med el 525.0 Bondegatan 8, 78441, Borlänge
TgV47vfp46yhcgjhBBtXD88w Kometgatan 10 Garageplats med el 357.0 Kometgatan 10, 78453, Borlänge
f4tGTVQpPRh4hB76T7FQvmXP Kometgatan 1 Garageplats med el 357.0 Kometgatan 1, 78453, Borlänge
dCMwBGjBDCwV9qp4cjBWmyhp Allfarvägen 51 Enskilt varmgarage 507.0 Allfarvägen 51, 78442, Borlänge
vvw84wRBrvdTdGv6GdRrBC7R Tunagatan 13 Plats i varmgarage. 425.0 Tunagatan 13, 78434, Borlänge
prQ8pf4XhjwWx6fqGGpkpMHK Bondegatan 6 Enskilt varmgarage 496.0 Bondegatan 6, 78441, Borlänge
fqpxB79RFF6tv4kQmyhryYcf Bondegatan 10 Garageplats med el 525.0 Bondegatan 10, 78441, Borlänge
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment