最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

url is not working in python, but working in browser - Stack Overflow

programmeradmin2浏览0评论

.0/ttarrivals.aspx?key=API_KEY&mapid=40380&outputType=JSON

The CTA API documentation is at />

It succeeds (HTTP status 200) in the browser, but in Python, using requests.get, the server returns 403.

I tried with header and sessions but I still received a 403 status.

url = ".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)

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

The CTA API documentation is at https://www.transitchicago/developers/ttdocs/>

It succeeds (HTTP status 200) in the browser, but in Python, using requests.get, the server returns 403.

I tried with header and sessions but I still received a 403 status.

url = "https://lapi.transitchicago/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)
Share Improve this question edited Mar 17 at 10:09 VLAZ 29.1k9 gold badges63 silver badges84 bronze badges asked Mar 14 at 19:59 usamausama 157 bronze badges 2
  • Hey, is your API key correct? Also please try adding this line to your headers 'Accept': 'application/json',. – Yaniek Commented Mar 14 at 21:14
  • If you tried the code in my answer and still got a 403 error then I deduce that your API key must be invalid – Adon Bilivit Commented Mar 17 at 7:44
Add a comment  | 

1 Answer 1

Reset to default 0

Given a valid API key the following will work without error.

import requests
import json
from apikeys import CTA_API_KEY

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

params = {
    "key": CTA_API_KEY,
    "mapid": 40380,
    "outputType": "JSON",
    "max": 1
}

with requests.Session() as session:
    with session.get(URL, params=params) as response:
        response.raise_for_status()
        j = response.json()
        print(json.dumps(j, indent=2))

Output:

{
  "ctatt": {
    "tmst": "2025-03-15T01:57:46",
    "errCd": "0",
    "errNm": null,
    "eta": [
      {
        "staId": "40380",
        "stpId": "30374",
        "staNm": "Clark/Lake",
        "stpDe": "Subway service toward Forest Park",
        "rn": "225",
        "rt": "Blue",
        "destSt": "0",
        "destNm": "UIC-Halsted",
        "trDr": "5",
        "prdt": "2025-03-15T01:57:37",
        "arrT": "2025-03-15T02:00:37",
        "isApp": "0",
        "isSch": "0",
        "isDly": "0",
        "isFlt": "0",
        "flags": null,
        "lat": "41.89119",
        "lon": "-87.64758",
        "heading": "123"
      }
    ]
  }
}

Note that this API doesn't require explicit headers

发布评论

评论列表(0)

  1. 暂无评论