# Wikidata SPARQL Scraper - Query the Knowledge Graph (`ninhothedev/wikidata-sparql-scraper`) Actor

$0.5/1K 🔥 Wikidata SPARQL scraper! Query the world knowledge graph — cities, laureates, UNESCO sites & custom queries. No key. JSON, CSV, Excel or API in seconds. Power research & RAG ⚡

- **URL**: https://apify.com/ninhothedev/wikidata-sparql-scraper.md
- **Developed by:** [ninhothedev](https://apify.com/ninhothedev) (community)
- **Categories:** AI, Developer tools
- **Stats:** 2 total users, 1 monthly users, 100.0% runs succeeded, 0 bookmarks
- **User rating**: No ratings yet

## Pricing

from $0.50 / 1,000 results

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/platform/actors/running/actors-in-store#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

## Wikidata SPARQL Scraper - Query the World Knowledge Graph (No API Key)

Run **any SPARQL query** against [Wikidata](https://www.wikidata.org), the free structured knowledge graph behind Wikipedia, and get back clean, flat, tabular rows. Every SPARQL variable becomes its own column, entity URIs are resolved to bare QIDs, and coordinates are split into latitude/longitude - ready for CSV, Excel, pandas, a vector store, or a database.

Six hand-tuned **preset queries** ship with the actor, so you can pull useful datasets (largest cities, Nobel laureates, UNESCO World Heritage sites, tallest buildings, GDP by country, programming languages) without writing a single line of SPARQL.

**No API key. No login. No proxy required.** Wikidata's Query Service is free and open.

***

### How is this different from the Wikidata Scraper?

This actor is the **query counterpart** to [`ninhothedev/wikidata-scraper`](https://apify.com/ninhothedev/wikidata-scraper).

| | **Wikidata Scraper** (`wikidata-scraper`) | **Wikidata SPARQL Scraper** (this actor) |
|---|---|---|
| Purpose | **Entity lookup** - fetch known items | **Structured querying** - discover unknown items |
| You give it | QIDs, or a keyword search | A SPARQL query (or a preset) |
| You get back | One row per entity, with its labels, descriptions, aliases, claims and sitelinks | One row per query result binding, with one column per SPARQL variable |
| Typical question | *"What does Wikidata know about Q42?"* | *"Give me every city over 10M people with its coordinates."* |
| Filtering | By entity ID / search term | By any graph pattern: type, property, value, date range, ranking, aggregation |
| Best for | Enriching a list of entities you already have | Building a dataset from scratch out of the knowledge graph |

**Rule of thumb:** if you already know *which* things you want, use `wikidata-scraper`. If you want the graph to *find* the things for you (by type, property, ranking or relationship), use this one.

***

### What you can do with it

- **Build research datasets** - every UNESCO site with coordinates, every Nobel laureate by decade, every country's GDP over time. Reproducible and citable, straight from the source.
- **Enrich RAG / LLM pipelines** - pull authoritative structured facts (dates, coordinates, identifiers, relationships) to ground a model and cut hallucinations. Each row keeps its QID so you can link back to canonical entities.
- **Knowledge graph work** - export subgraphs, map external identifiers (ISO codes, VIAF, GND, IMDb, ORCID) to QIDs, or seed your own graph database.
- **Data journalism** - answer questions like *"which heads of state studied abroad"* or *"how have skyscraper heights grown per decade"* with a single query, and get a spreadsheet back.
- **Entity resolution & reference data** - generate clean lookup tables of countries, languages, currencies, professions, chemical compounds, species, or anything else Wikidata models.

***

### Input

| Field | Type | Default | Description |
|---|---|---|---|
| `mode` | select: `presets` / `sparql` | `presets` | `presets` runs one of the six built-in queries. `sparql` runs your own query. |
| `preset` | select | `largest_cities` | Which built-in query to run (only in `presets` mode). |
| `query` | string | - | Your SPARQL SELECT statement (only in `sparql` mode). |
| `maxItems` | integer | `200` (max `2000`) | Maximum result rows. Also injected as the `LIMIT` for presets and for custom queries that have none. |

#### Example - run a preset

```json
{
  "mode": "presets",
  "preset": "largest_cities",
  "maxItems": 500
}
```

#### Example - run your own SPARQL

```json
{
  "mode": "sparql",
  "query": "SELECT ?country ?countryLabel ?capital ?capitalLabel ?coord WHERE { ?country wdt:P31 wd:Q6256 ; wdt:P36 ?capital . OPTIONAL { ?capital wdt:P625 ?coord . } SERVICE wikibase:label { bd:serviceParam wikibase:language \"en\". } } ORDER BY ?countryLabel",
  "maxItems": 200
}
```

***

### The six built-in presets

Every preset is tuned to return fast (well inside Wikidata's 60-second limit) and to include labels, identifiers and coordinates where they exist.

#### 1. `largest_cities`

The most populous cities on earth, ranked by population.
**Columns:** `city`, `cityLabel`, `cityDescription`, `population`, `country`, `countryLabel`, `coord`, `coord_lat`, `coord_lon`
*Use for:* geo datasets, market sizing, map visualisations, city reference tables.

#### 2. `nobel_laureates`

Nobel Prize winners across all categories, newest first.
**Columns:** `laureate`, `laureateLabel`, `laureateDescription`, `award`, `awardLabel` (the prize category), `year`, `countryLabel` (citizenship)
*Use for:* science history analysis, prize-by-country studies, biography enrichment.

#### 3. `unesco_sites`

UNESCO World Heritage sites worldwide.
**Columns:** `site`, `siteLabel`, `siteDescription`, `country`, `countryLabel`, `coord`, `coord_lat`, `coord_lon`, `inscribed` (year added to the list)
*Use for:* travel and tourism products, cultural heritage research, map layers.

#### 4. `tallest_buildings`

Completed buildings ranked by height in metres (unbuilt proposals and obvious data-entry outliers are filtered out by requiring an opening date and a plausible 150-1200 m height).
**Columns:** `building`, `buildingLabel`, `height`, `opened`, `city`, `cityLabel`, `country`, `countryLabel`, `coord`, `coord_lat`, `coord_lon`
*Use for:* architecture datasets, construction trend analysis, city skylines.

#### 5. `countries_gdp`

Sovereign states ranked by nominal GDP.
**Columns:** `country`, `countryLabel`, `iso` (ISO 3166-1 alpha-3), `gdp`, `gdpDate` (the year the figure refers to), `population`, `capitalLabel`
*Use for:* economic dashboards, country reference tables, ISO-code mapping.

#### 6. `programming_languages`

Programming languages ordered by inception date - the history of computing in one table.
**Columns:** `lang`, `langLabel`, `langDescription`, `inception`, `creator`, `creatorLabel`, `paradigmLabel`, `influencedByLabel`
*Use for:* tech history, developer content, language family trees.

***

### Writing your own SPARQL

Wikidata SPARQL is easier than it looks. Three building blocks cover most queries:

- `wd:Q…` - an **entity** (a thing). `wd:Q515` = city, `wd:Q6256` = country, `wd:Q5` = human.
- `wdt:P…` - a **property** (a relationship). `wdt:P31` = *instance of*, `wdt:P17` = *country*, `wdt:P1082` = *population*, `wdt:P625` = *coordinates*, `wdt:P569` = *date of birth*.
- The **label service** - add this line and every `?var` automatically gains a readable `?varLabel` (and `?varDescription`):

```sparql
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
```

A minimal query - all humans who are astronauts, with their birth dates:

```sparql
SELECT ?person ?personLabel ?birth WHERE {
  ?person wdt:P106 wd:Q11631 ;      # occupation: astronaut
          wdt:P569  ?birth .        # date of birth
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
ORDER BY ?birth
LIMIT 200
```

Useful patterns:

- **Optional data:** wrap properties that not every item has in `OPTIONAL { … }` so rows are not dropped.
- **Subclasses:** `?x wdt:P31/wdt:P279* wd:Q11303` matches anything that is a skyscraper *or a subtype of one*. Powerful, but slow - always pair it with a `LIMIT` inside a subquery.
- **Qualifiers:** use `p:` / `ps:` / `pq:` to reach statement-level data, e.g. the year attached to an award: `?p p:P166 ?st . ?st ps:P166 ?award ; pq:P585 ?date .`
- **Ranking:** `ORDER BY DESC(?population) LIMIT 100`.
- **Aggregation:** `SELECT ?countryLabel (COUNT(?city) AS ?cities) … GROUP BY ?countryLabel`.

Tips and limits:

- Only **SELECT** queries are supported (`ASK`, `CONSTRUCT` and `DESCRIBE` do not produce tabular bindings).
- No `PREFIX` lines are needed - the endpoint predefines `wd:`, `wdt:`, `p:`, `ps:`, `pq:`, `wikibase:`, `rdfs:` and friends.
- Wikidata enforces a **hard 60-second timeout**. If you hit it the actor tells you so; add a `LIMIT`, drop expensive `OPTIONAL` blocks, or narrow the property path.
- Prototype interactively at [query.wikidata.org](https://query.wikidata.org) - it has autocomplete and a query-example gallery - then paste the working query in here.

***

### Output

One dataset item per SPARQL result binding. Because the columns depend on your query, the flattened variables are merged into the **top level** of the row *and* the untouched binding is preserved under `raw`.

Fixed fields on every row:

| Field | Description |
|---|---|
| `query_name` | The preset name, or `"custom"` for `sparql` mode |
| `qids` | Object mapping each variable that held a Wikidata entity URI to its bare QID/PID |
| `raw` | The complete original SPARQL binding, exactly as returned |
| `source` | Always `"wikidata"` |
| `scraped_at` | UTC ISO-8601 timestamp of the run |

Plus **one column per SPARQL variable**, flattened from the SPARQL-JSON `{"type": …, "value": …}` cell to a plain value (numeric datatypes become numbers, booleans become booleans). Coordinate values additionally produce `<var>_lat` and `<var>_lon`. All fields are nullable - unbound optional variables appear as `null`.

Real sample row (`largest_cities`):

```json
{
  "query_name": "largest_cities",
  "city": "http://www.wikidata.org/entity/Q956",
  "cityLabel": "Beijing",
  "population": 21893095,
  "country": "http://www.wikidata.org/entity/Q148",
  "countryLabel": "People's Republic of China",
  "coord": "Point(116.407526 39.90403)",
  "coord_lat": 39.90403,
  "coord_lon": 116.407526,
  "qids": { "city": "Q956", "country": "Q148" },
  "raw": { "city": { "type": "uri", "value": "http://www.wikidata.org/entity/Q956" }, "...": "..." },
  "source": "wikidata",
  "scraped_at": "2026-07-28T13:41:07.512834+00:00"
}
```

Export as JSON, CSV, Excel, XML or HTML from the Apify UI or API.

***

### Pricing

Roughly **$0.50 per 1,000 result rows** on Apify's pay-per-event style compute. The actor runs on 512 MB and a single query returning 2,000 rows finishes in seconds, so most runs cost a fraction of a cent.

### Notes on fair use

The Wikidata Query Service is a free public service run by the Wikimedia Foundation. This actor sends a descriptive User-Agent (required - anonymous requests get HTTP 403), retries transient gateway errors with backoff, and issues exactly **one** query per run. Keep `maxItems` reasonable and avoid hammering the endpoint with heavy queries. Wikidata content is published under [CC0](https://creativecommons.org/publicdomain/zero/1.0/) - free for any use, attribution appreciated.

### Related actors

- [Wikidata Scraper](https://apify.com/ninhothedev/wikidata-scraper) - entity lookup by QID or search term
- [Wikipedia Scraper](https://apify.com/ninhothedev/wikipedia-scraper) - article text, summaries and metadata
- [OpenAlex Scraper](https://apify.com/ninhothedev/openalex-scraper) - open scholarly works, authors and institutions
- [GLEIF Scraper](https://apify.com/ninhothedev/gleif-scraper) - global LEI legal-entity reference data

### Support

Found a bug or need another preset? Open an issue on the actor page.

# Actor input Schema

## `mode` (type: `string`):

How the query is chosen. 'presets' runs one of the six built-in, hand-tuned queries (pick it with the Preset field). 'sparql' runs your own SPARQL SELECT statement from the Query field against the Wikidata Query Service.

## `preset` (type: `string`):

Which built-in query to run (used only when Mode = presets). largest\_cities = most populous cities with population, country and coordinates. nobel\_laureates = Nobel Prize winners with category and year. unesco\_sites = UNESCO World Heritage sites with country and coordinates. tallest\_buildings = completed buildings by height with opening date and city. countries\_gdp = sovereign states ranked by nominal GDP with ISO code and population. programming\_languages = languages with inception date, creator and paradigm.

## `query` (type: `string`):

Your own SPARQL SELECT query, used only when Mode = sparql. Wikidata prefixes (wd:, wdt:, p:, ps:, pq:, wikibase:, rdfs:) are predefined by the endpoint, so no PREFIX lines are needed. Add SERVICE wikibase:label { bd:serviceParam wikibase:language "en". } to get ?varLabel columns. If you do not supply a LIMIT, the Max items value is appended automatically. Only SELECT queries are supported and the service enforces a hard 60-second timeout.

## `maxItems` (type: `integer`):

Maximum number of result rows (SPARQL bindings) to push to the dataset. Also used as the LIMIT injected into preset queries and into custom queries that have no LIMIT of their own. Keep this low for expensive queries so the Wikidata 60-second timeout is not hit.

## Actor input object example

```json
{
  "mode": "presets",
  "preset": "largest_cities",
  "query": "SELECT ?country ?countryLabel ?capital ?capitalLabel ?coord WHERE {\n  ?country wdt:P31 wd:Q6256 ;\n           wdt:P36 ?capital .\n  OPTIONAL { ?capital wdt:P625 ?coord . }\n  SERVICE wikibase:label { bd:serviceParam wikibase:language \"en\". }\n}\nORDER BY ?countryLabel\nLIMIT 200",
  "maxItems": 200
}
```

# 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 = {
    "query": `SELECT ?country ?countryLabel ?capital ?capitalLabel ?coord WHERE {
  ?country wdt:P31 wd:Q6256 ;
           wdt:P36 ?capital .
  OPTIONAL { ?capital wdt:P625 ?coord . }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
ORDER BY ?countryLabel
LIMIT 200`
};

// Run the Actor and wait for it to finish
const run = await client.actor("ninhothedev/wikidata-sparql-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 = { "query": """SELECT ?country ?countryLabel ?capital ?capitalLabel ?coord WHERE {
  ?country wdt:P31 wd:Q6256 ;
           wdt:P36 ?capital .
  OPTIONAL { ?capital wdt:P625 ?coord . }
  SERVICE wikibase:label { bd:serviceParam wikibase:language \"en\". }
}
ORDER BY ?countryLabel
LIMIT 200""" }

# Run the Actor and wait for it to finish
run = client.actor("ninhothedev/wikidata-sparql-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 '{
  "query": "SELECT ?country ?countryLabel ?capital ?capitalLabel ?coord WHERE {\\n  ?country wdt:P31 wd:Q6256 ;\\n           wdt:P36 ?capital .\\n  OPTIONAL { ?capital wdt:P625 ?coord . }\\n  SERVICE wikibase:label { bd:serviceParam wikibase:language \\"en\\". }\\n}\\nORDER BY ?countryLabel\\nLIMIT 200"
}' |
apify call ninhothedev/wikidata-sparql-scraper --silent --output-dataset

```

## MCP server setup

```json
{
    "mcpServers": {
        "apify": {
            "type": "http",
            "url": "https://mcp.apify.com/?tools=fetch-actor-details,ninhothedev/wikidata-sparql-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/tAHPoafRQxrZNMRYu/builds/6ftprgnHUmtEyyFv2/openapi.json
