# Deutsche Bahn Scraper - Train Schedules & Prices (`studio-amba/deutsche-bahn-scraper`) Actor

Scrape train connections, schedules, and prices from Deutsche Bahn (bahn.de). Extract ICE, IC, RE, and S-Bahn routes with departure/arrival times, duration, transfers, carriers, and live fares across Germany and Europe.

- **URL**: https://apify.com/studio-amba/deutsche-bahn-scraper.md
- **Developed by:** [Studio Amba](https://apify.com/studio-amba) (community)
- **Categories:** Travel
- **Stats:** 2 total users, 0 monthly users, 96.7% runs succeeded, 0 bookmarks
- **User rating**: No ratings yet

## Pricing

from $2.00 / 1,000 result scrapeds

This Actor is paid per event. You are not charged for the Apify platform usage, but only a fixed price for specific events.

Learn more: https://docs.apify.com/actors/running/actors-in-store.md#pay-per-event

## What's an Apify Actor?

Actors are web data automations that power AI and operations. They run on the Apify platform to scrape websites, process data, connect APIs, and automate workflows.
In Batch mode, an Actor accepts a well-defined JSON input, performs an action which can take anything from a few seconds to a few hours,
and optionally produces a well-defined JSON output, datasets with results, or files in key-value store.
In Standby mode, an Actor provides a web server which can be used as a website, API, or an MCP server.
Actors are written with capital "A".

## How to integrate an Actor?

If asked about integration, you help developers integrate Actors into their projects.
You adapt to their stack and deliver integrations that are safe, well-documented, and production-ready.
The best way to integrate Actors is as follows.

- **AI agents and MCP clients** — the [Apify MCP server](https://docs.apify.com/integrations/mcp.md) at `https://mcp.apify.com` (remote, streamable HTTP, OAuth on first use).
- **Agentic workflows and local Actor development** — [Agent Skills](https://apify.com/.well-known/agent-skills/index.json) with the [Apify CLI](https://docs.apify.com/cli/docs.md): `npm install -g apify-cli`, then `apify login`.
- **JavaScript/TypeScript projects** — the official [JS/TS client](https://docs.apify.com/api/client/js/docs.md): `npm install apify-client`.
- **Python projects** — the official [Python client](https://docs.apify.com/api/client/python/docs.md): `pip install apify-client`.
- **Any other language** — the [REST API](https://docs.apify.com/api/v2.md).

For usage examples, see the [API](#api) section below.

For more details, see Apify documentation as [Markdown index](https://docs.apify.com/llms.txt) and [Markdown full-text](https://docs.apify.com/llms-full.txt).

# README

## Deutsche Bahn Scraper

Scrape **train connections, schedules, and prices** from [Deutsche Bahn](https://www.bahn.de) (bahn.de), Germany's national railway operator with over 50 million monthly visitors. Extract ICE, IC, RE, RB, and S-Bahn routes with departure/arrival times, durations, transfer counts, platform numbers, carrier information, and live ticket fares.

No login or cookies required. The actor handles bahn.de's complex SPA and anti-bot protections automatically using residential proxies and a headless browser.

### How to scrape Deutsche Bahn data

1. Go to [Deutsche Bahn Scraper](https://apify.com/studio-amba/deutsche-bahn-scraper) on the Apify Store.
2. Click **Try for free** to open the actor in Apify Console.
3. Enter your **origin** and **destination** stations (e.g., "Berlin Hbf" to "München Hbf").
4. Optionally set a departure date and time.
5. Click **Start** and wait for the results.
6. Download your data in JSON, CSV, Excel, or connect it directly to your workflow via API.

### Why use this actor?

- **Comprehensive data** — Get full connection details including train types, prices, platforms, transfer counts, and individual journey legs in a single run.
- **No login needed** — Extracts publicly available schedule and pricing data without any Deutsche Bahn account.
- **Handles the SPA** — bahn.de is a complex single-page application. This actor uses Playwright to properly render the page and extract data that simple HTTP scrapers miss.
- **Scheduled runs** — Set up daily or hourly schedules to monitor price changes on your preferred routes.
- **API access** — Integrate train data directly into your applications, dashboards, or business workflows using the Apify API.

### Input

| Parameter | Type | Default | Description |
|-----------|------|---------|-------------|
| `origin` | string | `Berlin Hbf` | Departure station name. Partial names are resolved automatically. |
| `destination` | string | `München Hbf` | Arrival station name. Partial names are resolved automatically. |
| `departureDate` | string | Tomorrow | Search date in `YYYY-MM-DD` format. |
| `departureTime` | string | `08:00` | Earliest departure time in `HH:MM` format (24h). |
| `travelClass` | string | `2` | Travel class: `1` (First) or `2` (Second). |
| `maxResults` | integer | `20` | Maximum number of connections to return (1-200). |
| `proxyConfiguration` | object | Residential DE | Proxy settings. Residential proxies with a German exit node are recommended. |

#### Example input

```json
{
    "origin": "Berlin Hbf",
    "destination": "München Hbf",
    "departureDate": "2026-06-15",
    "departureTime": "08:00",
    "travelClass": "2",
    "maxResults": 20,
    "proxyConfiguration": {
        "useApifyProxy": true,
        "apifyProxyGroups": ["RESIDENTIAL"],
        "apifyProxyCountry": "DE"
    }
}
```

### Output

The actor produces a dataset with one item per train connection. You can download the dataset in various formats such as JSON, HTML, CSV, or Excel.

#### Example output

```json
{
    "origin": "Berlin Hbf",
    "destination": "München Hbf",
    "departureTime": "2026-06-15T08:15:00+02:00",
    "arrivalTime": "2026-06-15T12:10:00+02:00",
    "duration": "3h 55min",
    "durationMinutes": 235,
    "trainType": "ICE",
    "trainNumber": "ICE 1001",
    "price": 29.90,
    "currency": "EUR",
    "travelClass": "2",
    "transfers": 0,
    "legs": [
        {
            "origin": "Berlin Hbf",
            "destination": "München Hbf",
            "departureTime": "2026-06-15T08:15:00+02:00",
            "arrivalTime": "2026-06-15T12:10:00+02:00",
            "trainType": "ICE",
            "trainNumber": "ICE 1001",
            "carrier": "DB Fernverkehr",
            "departurePlatform": "6",
            "arrivalPlatform": "12"
        }
    ],
    "carrier": "DB Fernverkehr",
    "departurePlatform": "6",
    "arrivalPlatform": "12",
    "url": "https://int.bahn.de/en/buchung/fahrplanauskunft?...",
    "scrapedAt": "2026-06-09T14:30:00.000Z"
}
```

### Data fields

| Field | Type | Description |
|-------|------|-------------|
| `origin` | string | Departure station name |
| `destination` | string | Arrival station name |
| `departureTime` | string | ISO 8601 departure timestamp |
| `arrivalTime` | string | ISO 8601 arrival timestamp |
| `duration` | string | Human-readable duration (e.g., "3h 55min") |
| `durationMinutes` | number | Duration in minutes for easy sorting/filtering |
| `trainType` | string | Primary train category: ICE, IC/EC, RE/RB, S-Bahn, TGV, etc. |
| `trainNumber` | string | Full train identifier (e.g., "ICE 1001") |
| `price` | number | Ticket price in EUR (0 if not available) |
| `currency` | string | Always "EUR" |
| `travelClass` | string | Travel class searched (1 or 2) |
| `transfers` | number | Number of transfers/changes |
| `legs` | array | Individual trip segments with per-leg train details |
| `carrier` | string | Operating railway company (e.g., "DB Fernverkehr") |
| `departurePlatform` | string | Departure platform/track number |
| `arrivalPlatform` | string | Arrival platform/track number |
| `url` | string | Direct link to this connection on bahn.de |
| `scrapedAt` | string | ISO 8601 timestamp of when the data was collected |

### How much does it cost to scrape Deutsche Bahn?

The actor uses Playwright with residential proxies, and that cost is folded into the actor's per-event price, not billed separately. It costs $0.02 per run plus $0.002 per connection result — a typical search returning 20 connections costs about $0.06. Running daily monitoring on a single route costs roughly $1.80 per month. Usage cost only settles once a run reports SUCCEEDED.

For high-volume usage, increase `maxResults` and use the pagination feature to get more connections per run rather than running multiple separate searches.

### Tips

- **Use German station names** for best results. "München Hbf" works better than "Munich Central".
- **Prices may not always be available** — Deutsche Bahn shows prices dynamically and some connections may return a price of 0. This typically means the fare is not yet released or sold out.
- **Schedule runs** during off-peak hours for the most reliable results.
- **Residential proxies** with a German exit node (`DE`) are strongly recommended. bahn.de has Akamai protection that blocks datacenter IPs.
- **Search ahead** — Deutsche Bahn releases tickets 6 months in advance. Earlier searches tend to show lower "Sparpreis" fares.

### FAQ

#### Is it legal to scrape Deutsche Bahn?

This actor extracts publicly available train schedule and pricing data from bahn.de. The data is accessible to any visitor without login. Always review Deutsche Bahn's Terms of Service and ensure your use case complies with applicable laws and regulations.

#### Why are some prices showing as 0?

Deutsche Bahn loads prices dynamically and some fare classes may not be available for all connections. A price of 0 means the fare was not displayed on the search results page. Try searching for a closer departure date when prices are more likely to be published.

#### The actor returned no results — what should I do?

- Make sure your station names are valid German station names (e.g., "Frankfurt (Main) Hbf" not "Frankfurt Airport").
- Check that your departure date is in the future.
- Ensure you are using residential proxies with a German exit node.
- Try a popular route like "Berlin Hbf" to "München Hbf" to verify the actor is working.

#### Can I scrape international routes?

Yes, Deutsche Bahn covers international connections to Austria, Switzerland, France, Netherlands, Belgium, Czech Republic, Poland, Denmark, and more. Enter the destination as it appears on bahn.de (e.g., "Wien Hbf", "Zürich HB", "Paris Est").

### Support

If you encounter any issues or have feature requests, please open an issue in the [Issues tab](https://console.apify.com/actors/studio-amba~deutsche-bahn-scraper/issues). For custom solutions or bulk data needs, reach out via the Apify platform.

# Actor input Schema

## `origin` (type: `string`):

Departure station name (e.g. 'Berlin Hbf', 'Hamburg', 'Frankfurt am Main'). The actor resolves partial names automatically.

## `destination` (type: `string`):

Arrival station name (e.g. 'München Hbf', 'Köln', 'Dresden'). The actor resolves partial names automatically.

## `departureDate` (type: `string`):

Date to search for connections in YYYY-MM-DD format. Defaults to tomorrow if not set.

## `departureTime` (type: `string`):

Earliest departure time in HH:MM format (24h). Defaults to 08:00.

## `travelClass` (type: `string`):

Ticket class to search for.

## `maxResults` (type: `integer`):

Maximum number of train connections to return.

## `proxyConfiguration` (type: `object`):

Proxy settings. The actor uses the public HAFAS API, so datacenter proxies usually work fine. Use residential proxies only if you experience rate limiting.

## Actor input object example

```json
{
  "origin": "Berlin Hbf",
  "destination": "München Hbf",
  "departureTime": "08:00",
  "travelClass": "2",
  "maxResults": 20,
  "proxyConfiguration": {
    "useApifyProxy": true,
    "apifyProxyGroups": [
      "RESIDENTIAL"
    ],
    "apifyProxyCountry": "DE"
  }
}
```

# Actor output Schema

## `results` (type: `string`):

No description

# API

You can run this Actor programmatically using our API. Below are code examples in JavaScript, Python, and CLI, as well as the OpenAPI specification and MCP server setup.

## JavaScript example

```javascript
import { ApifyClient } from 'apify-client';

// Initialize the ApifyClient with your Apify API token
// Replace the '<YOUR_API_TOKEN>' with your token
const client = new ApifyClient({
    token: '<YOUR_API_TOKEN>',
});

// Prepare Actor input
const input = {
    "origin": "Berlin Hbf",
    "destination": "München Hbf",
    "departureDate": "",
    "departureTime": "08:00",
    "maxResults": 20,
    "proxyConfiguration": {
        "useApifyProxy": true,
        "apifyProxyGroups": [
            "RESIDENTIAL"
        ],
        "apifyProxyCountry": "DE"
    }
};

// Run the Actor and wait for it to finish
const run = await client.actor("studio-amba/deutsche-bahn-scraper").call(input);

// Fetch and print Actor results from the run's dataset (if any)
console.log('Results from dataset');
console.log(`💾 Check your data here: https://console.apify.com/storage/datasets/${run.defaultDatasetId}`);
const { items } = await client.dataset(run.defaultDatasetId).listItems();
items.forEach((item) => {
    console.dir(item);
});

// 📚 Want to learn more 📖? Go to → https://docs.apify.com/api/client/js/docs

```

## Python example

```python
from apify_client import ApifyClient

# Initialize the ApifyClient with your Apify API token
# Replace '<YOUR_API_TOKEN>' with your token.
client = ApifyClient("<YOUR_API_TOKEN>")

# Prepare the Actor input
run_input = {
    "origin": "Berlin Hbf",
    "destination": "München Hbf",
    "departureDate": "",
    "departureTime": "08:00",
    "maxResults": 20,
    "proxyConfiguration": {
        "useApifyProxy": True,
        "apifyProxyGroups": ["RESIDENTIAL"],
        "apifyProxyCountry": "DE",
    },
}

# Run the Actor and wait for it to finish
run = client.actor("studio-amba/deutsche-bahn-scraper").call(run_input=run_input)

# Fetch and print Actor results from the run's dataset (if there are any)
print(f"💾 Check your data here: https://console.apify.com/storage/datasets/{run.default_dataset_id}")
for item in client.dataset(run.default_dataset_id).iterate_items():
    print(item)

# 📚 Want to learn more 📖? Go to → https://docs.apify.com/api/client/python/docs/quick-start

```

## CLI example

```bash
echo '{
  "origin": "Berlin Hbf",
  "destination": "München Hbf",
  "departureDate": "",
  "departureTime": "08:00",
  "maxResults": 20,
  "proxyConfiguration": {
    "useApifyProxy": true,
    "apifyProxyGroups": [
      "RESIDENTIAL"
    ],
    "apifyProxyCountry": "DE"
  }
}' |
apify call studio-amba/deutsche-bahn-scraper --silent --output-dataset

```

## MCP server setup

```json
{
    "mcpServers": {
        "apify": {
            "type": "http",
            "url": "https://mcp.apify.com/?tools=fetch-actor-details,studio-amba/deutsche-bahn-scraper"
        }
    }
}

```

The hosted server signs you in with OAuth on first connect, so no API token belongs in this config. Clients without OAuth support can send an `Authorization: Bearer <APIFY_API_TOKEN>` header instead, using a token from API & Integrations in Apify Console (https://console.apify.com/settings/integrations).

## OpenAPI specification

Download the OpenAPI definition: https://api.apify.com/v2/actors/TKuNgZiLWRCrBNBEn/builds/sSttesfoVJJ7VSWrD/openapi.json
