[Python] url is not working in python, but working in browser

Discussão em 'Python' iniciado por Stack, Março 14, 2025.

  1. Stack

    Stack Membro Participativo

    https://lapi.transitchicago.com/api/1.0/ttarrivals.aspx?key=API_KEY&mapid=40380&outputType=JSON

    This is a CTA API documentation https://www.transitchicago.com/developers/ttdocs/>

    It works in browser but in python not working and returning 403.

    I tried with header and sessions but not working

    url = "https://lapi.transitchicago.com/api/1.0/ttarrivals.aspx"

    # Request parameters
    params = {
    'key': api_key,
    'mapid': station_id,
    'max': max_results,
    'outputType': output_type
    }

    # Headers to mimic a browser
    headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
    'Accept': 'application/json',
    }

    try:
    # Make the API request
    response = requests.get(url, params=params, headers=headers)



    headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3',
    'Accept-Language': 'en-US,en;q=0.9',
    'Accept-Encoding': 'gzip, deflate, br',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
    'Connection': 'keep-alive',
    'Upgrade-Insecure-Requests': '1'
    }

    session = requests.Session()
    session.headers.update(headers)
    response = session.get(url)

    Continue reading...

Compartilhe esta Página