使用Python从URL中删除JSON

bd1hkmkf  于 2023-10-21  发布在  Python
关注(0)|答案(1)|浏览(86)

我想从一个网站检索一个JSON,但不知何故它不工作。我可以在网站上看到JSON(开发人员工具)并验证它,一切都很好。
下面是我的代码:

import requests
from requests.exceptions import HTTPError

try:
    response = requests.get('https://www.landal.de/parks/beach-park-texel/unterkunfte/4bl')
    response.raise_for_status()
    # access JSOn content
    jsonResponse = response.json()
    print("Entire JSON response")
    print(jsonResponse)

except HTTPError as http_err:
    print(f'HTTP error occurred: {http_err}')
except Exception as err:
    print(f'Other error occurred: {err}')

错误如下:

Other error occurred: Expecting value: line 2 column 1 (char 2)

当我读了很多问题,我感觉这是网站。

doinxwow

doinxwow1#

要从页面中获取JSON数据,请使用POST和payload:

import requests

url = "https://www.landal.de/parks/beach-park-texel/unterkunfte/4bl"

payload = {
    "arrivalDate": "20-11-2023",
    "departureDate": "24-11-2023",
    "numberOfDays": "5",
    "stayType": "925",
    "travelGroup[0][Id]": "18-120",
    "travelGroup[0][Amount]": "0",
    "travelGroup[1][Id]": "3-17",
    "travelGroup[1][Amount]": "0",
    "travelGroup[2][Id]": "0-2",
    "travelGroup[2][Amount]": "0",
    "travelGroup[3][Id]": "pets",
    "travelGroup[3][Amount]": "0",
    "travelGroupCombiEnabled": "false",
    "scController": "PriceGridComponent",
    "scAction": "Get",
}

data = requests.post(url, data=payload).json()
print(data)

图纸:

{
    "IsSuccess": True,
    "searchResult": {
        "arrivalDates": [
            {
                "Year": "2023",
                "Month": "10",
                "DaysWithCount": [
                    {"Day": "06", "ParkCount": 1, "Durations": [3]},
                    {"Day": "13", "ParkCount": 1, "Durations": [3, 7, 10, 14]},
                    {"Day": "16", "ParkCount": 1, "Durations": [4, 7, 11, 14]},
                    {"Day": "20", "ParkCount": 1, "Durations": [3, 7, 10]},
                    {"Day": "23", "ParkCount": 1, "Durations": [4, 7]},
                    {"Day": "27", "ParkCount": 1, "Durations": [3]},
                ],
            },

...

相关问题