October 22, 2024
Chicago 12, Melborne City, USA
python

How do I get the json response from the API?


I am trying to read the property response from airbnb via the API but I somehow can’t get a proper result and I don’t know what I do wrong.

import json
from urllib.request import urlopen

def fetch_airbnb_data():
    api_url = "https://www.airbnb.co.uk/api/v3/StaysPdpSections/3bcbe9ee20da9daa6722934f0b54ef68db41699d179e698c3aeab4ec30a964cb?operationName=StaysPdpSections&locale=en-GB&currency=GBP&variables=%7B%22id%22%3A%22U3RheUxpc3Rpbmc6MTAyNzQ1NzkyNDI2OTA2NDk4OQ%3D%3D%22%2C%22pdpSectionsRequest%22%3A%7B%22adults%22%3A%221%22%2C%22amenityFilters%22%3Anull%2C%22bypassTargetings%22%3Afalse%2C%22categoryTag%22%3Anull%2C%22children%22%3Anull%2C%22disasterId%22%3Anull%2C%22discountedGuestFeeVersion%22%3Anull%2C%22displayExtensions%22%3Anull%2C%22federatedSearchId%22%3Anull%2C%22forceBoostPriorityMessageType%22%3Anull%2C%22hostPreview%22%3Afalse%2C%22infants%22%3Anull%2C%22interactionType%22%3Anull%2C%22layouts%22%3A%5B%22SIDEBAR%22%2C%22SINGLE_COLUMN%22%5D%2C%22pets%22%3A0%2C%22pdpTypeOverride%22%3Anull%2C%22photoId%22%3Anull%2C%22preview%22%3Afalse%2C%22previousStateCheckIn%22%3Anull%2C%22previousStateCheckOut%22%3Anull%2C%22priceDropSource%22%3Anull%2C%22privateBooking%22%3Afalse%2C%22promotionUuid%22%3Anull%2C%22relaxedAmenityIds%22%3Anull%2C%22searchId%22%3Anull%2C%22selectedCancellationPolicyId%22%3Anull%2C%22selectedRatePlanId%22%3Anull%2C%22splitStays%22%3Anull%2C%22staysBookingMigrationEnabled%22%3Afalse%2C%22translateUgc%22%3Anull%2C%22useNewSectionWrapperApi%22%3Afalse%2C%22sectionIds%22%3Anull%2C%22checkIn%22%3Anull%2C%22checkOut%22%3Anull%2C%22p3ImpressionId%22%3A%22p3_1729488926_P3eUXvpmVbFyX_Cq%22%7D%7D&extensions=%7B%22persistedQuery%22%3A%7B%22version%22%3A1%2C%22sha256Hash%22%3A%223bcbe9ee20da9daa6722934f0b54ef68db41699d179e698c3aeab4ec30a964cb%22%7D%7D"
    
    try:
        jsonurl = urlopen(api_url)
        data = json.loads(jsonurl.read())
        print(json.dumps(data, indent=4))
    except Exception as e:
        print(f"Error fetching data: {e}")

fetch_airbnb_data()

I get the following error:

    "errors": [
        {
            "message": "Response not successful: Received status code 400",
            "locations": [
                {
                    "line": 1,
                    "column": 1
                }
            ],
            "path": [],
            "extensions": {
                "response": {
                    "statusCode": 400,
                    "body": {
                        "error_code": 400,
                        "error": "invalid_key",
                        "error_message": "Invalid API key."
                    }
                },
                "code": "Bad Request"
            }
        }
    ],
    "data": null
}```



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video