# Hacker News Data Scraper (`epctex/hackernews-scraper`) Actor

Extract Y Combinator's Hacker News based on any search criteria. Crawl the front page, Show HN, Ask HN, news, job listings, and historical data. Get links, titles, comments, ratings, and more!

- **URL**: https://apify.com/epctex/hackernews-scraper.md
- **Developed by:** [epctex](https://apify.com/epctex) (community)
- **Categories:** News
- **Features:** Limited permissions
- **Stats:** 202 total users, 13 monthly users, 100.0% runs succeeded, 16 bookmarks
- **User rating**: 5.00 out of 5 stars

## Pricing

from $0.30 / 1,000 results

This Actor is paid per event and usage. You are charged both the fixed price for specific events and for Apify platform usage.

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

## What's an Apify Actor?

An Actor is a serverless cloud program that runs on the Apify platform. It has two run modes.
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.

Apify vocabulary and the platform model are defined once, in the agent quickstart at https://apify.com/agents.md.

## 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.

Do not guess an integration path. Every one of them is in the agent quickstart at https://apify.com/agents.md: the Apify MCP server, Agent Skills with the Apify CLI, the JavaScript and Python clients, the REST API, and the account-free path for an agent with no human to sign in. It also carries the rule on stating cost before the first paid run.

For examples already wired to this Actor's own input schema, see the [API](#api) section below.

Each client library has reference documentation the quickstart does not restate: [JavaScript/TypeScript](https://docs.apify.com/api/client/js/docs.md) (`npm install apify-client`) and [Python](https://docs.apify.com/api/client/python/docs.md) (`pip install apify-client`).

# README

## Actor - Hacker News Scraper

### Hacker News scraper

Since Hacker News doesn't provide a good API, this actor should help you to retrieve data from it.

The Hacker News data scraper supports the following features:

- Scrape front page listing - You can scrape the homepage listings with any page you want.

- Scrape the newest listing - Latest news can be scraped right away from Hacker News.

- Scrape historical data - If you are looking for historical data, you can pick any date you want and scrape it over.

- Scrape listings of Ask HN - If you are specifically looking for an "Ask HN" type of listing, you can target it.

- Scrape listings of Show HN - If you are specifically looking for a "Show HN" type of listing, you can target it.

- Scrape listing details - You can scrape a single listing.

- Scrape Hacker News Algolia - You can retrieve everything from Hacker News' Algolia website

- Scrape job listings - You can scrape the latest job listings that are posted on Hacker News.

### Bugs, fixes, updates, and changelog

This scraper is under active development. If you have any feature requests you can create an issue from [here](https://github.com/epctex-support/hackernews-scraper/issues).

### Input Parameters

The input of this scraper should be JSON containing the list of pages on Hacker News that should be visited. Possible fields are:

- `startUrls`: (Optional) (Array) List of Hacker News URLs. You should only provide a news list, jobs list, or detail URLs.

- `mode`: (Optional) (String) Mode of the actor. Can be `FRONTPAGE`, `NEWEST`, `ASK`, `SHOW`, `JOBS` or `PAST`.

- `enableCommentHierarchy`: (Optional) (Boolean) Enables comment hierarchy over the posts. It retrieves all the comments and builds a tree within the replies.

- `endPage`: (Optional) (Number) Final number of page that you want to scrape. The default is `Infinite`. This applies to all `search` requests and `startUrls` individually.

- `maxItems`: (Optional) (Number) You can limit scraped items. This should be useful when you search through the big lists or search results.

- `proxy`: (Required) (Proxy Object) Proxy configuration.

- `extendOutputFunction`: (Optional) (String) Function that takes a JQuery handle ($) as an argument and returns an object with data.

This solution requires the use of **Proxy servers**, either your own proxy servers or you can use [Apify Proxy](https://www.apify.com/docs/proxy).

#### Tip

When you want to scrape over a specific listing URL, just copy and paste the link as one of the **startUrl**.

If you would like to scrape only the first page of a list then put the link for the page and have the `endPage` as 1.

With the last approach that is explained above you can also fetch any interval of pages. If you provide the 5th page of a list and define the `endPage` parameter as 6 then you'll have the 5th and 6th pages only.

If you would like to scrape historical data (ex: 2020-03-18) go to Hacker News, click on the "Past" tab, and find the URL that you are looking for. Then use the link as **startUrl**. Also; this is the format of historical data: `https://news.ycombinator.com/front?day=2020-03-18`

#### Compute Unit Consumption

The actor is optimized to run blazing fast and scrape many listings as possible. Therefore, it forefronts all listing detail requests. If the actor doesn't block very often it'll scrape 100 listings in 1 minute with ~0.03-0.04 compute units.

#### Hacker News Scraper Input example

```json
{
    "startUrls": [
        {
            "url": "https://news.ycombinator.com/item?id=26501527"
        },
        {
            "url": "https://news.ycombinator.com/front?day=2020-03-18"
        }
    ],
    "mode": "FRONTPAGE",
    "enableCommentHierarchy": false,
    "proxy": {
        "useApifyProxy": true
    },
    "endPage": 1,
    "maxItems": 100
}
```

### During the Run

During the run, the actor will output messages letting you know what is going on. Each message always contains a short label specifying which page from the provided list is currently specified.
When items are loaded from the page, you should see a message about this event with a loaded item count and total item count for each page.

If you provide incorrect input to the actor, it will immediately stop with a failure state and output an explanation of what is wrong.

### Hacker News Export

During the run, the actor stores results into a dataset. Each item is a separate item in the dataset.

You can manage the results in any language (Python, PHP, Node JS/NPM). See the FAQ or <a href="https://www.apify.com/docs/api" target="blank">our API reference</a> to learn more about getting results from this Hacker News actor.

### Scraped Hacker News Properties

The structure of each item in Hacker News listings looks like this:

#### Job Listings

```json
{
    "id": "26437893",
    "title": "Substack (YC W18) is hiring to build a better business model for writing",
    "link": "https://substack.com/jobs",
    "age": "7 days ago",
    "scrapedAt": "2021-03-19T21:56:00.085Z"
}
```

#### Single Comment

```json
{
    "scrapedType": "comment",
    "userName": "tsl54",
    "userLink": "https://news.ycombinator.com/user?id=tsl54",
    "age": "9 months ago",
    "message": "Congratulations on getting here! I’ve been part of a few management changes from the board level.",
    "comments":[]
},
```

#### News Listings

```json
{
    "id": "26501262",
    "title": "Fancy Defines",
    "link": "https://idiomdrottning.org/fancy-defines",
    "points": 15,
    "postedUserName": "todsacerdoti",
    "postedUserLink": "https://news.ycombinator.com/user?id=todsacerdoti",
    "numberOfComments": 1,
    "comments": [
        {
            "userName": "nerdponx",
            "userLink": "https://news.ycombinator.com/user?id=nerdponx",
            "age": "2 hours ago",
            "message": "Interesting, I didn't know about this at all. Is it that common in Scheme to write functions that immediately return other functions? Seems like an oddly \"blessed\" usage of syntax that IMO could be better used for something like pattern matching.Looking at this example from the linked SRFI [0]:    (define ((greet-with-prefix prefix) suffix)\n      (string-append prefix \" \" suffix))\n\n    (define greet (greet-with-prefix \"Hello\"))\n\n    (greet \"there!\") => \"Hello there!\"\n\nI'm not convinced that this is anything but an obfuscation, compared to the standard R5RS version:    (define (greet-with-prefix suffix)\n      (lambda (prefix)\n        (string-append prefix \" \" suffix)))\n\n    (define greet (greet-with-prefix \"Hello\"))\n\n    (greet \"there!\") => \"Hello there!\"\n\nWhat do the experienced Schemers think?[0]: https://srfi.schemers.org/srfi-219/srfi-219.html"
        }
    ],
    "age": "4 hours ago",
    "scrapedAt": "2021-03-19T21:37:45.462Z"
}
```

### Contact

Please visit us through [epctex.com](https://epctex.com) to see all the products that are available for you. If you are looking for any custom integration or so, please reach out to us through the chat box in [epctex.com](https://epctex.com). In need of support? <business@epctex.com> is at your service.

# Actor input Schema

## `startUrls` (type: `array`):

URLs to start with. It should be list or detail URL

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

Select mode of the actor

## `enableCommentHierarchy` (type: `boolean`):

Enables comment hierarchy. If disabled, all comments will be on the same level.

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

Maximum number of listing items that you want as output. Default is all

## `endPage` (type: `integer`):

The page number that you want to end with. By default there is no end page.

## `extendOutputFunction` (type: `string`):

Function that takes a JQuery handle ($) as argument and returns data that will be merged with the default output

## `proxy` (type: `object`):

Select proxies to be used by your crawler.

## Actor input object example

```json
{
  "startUrls": [
    {
      "url": "https://news.ycombinator.com/front"
    },
    {
      "url": "https://news.ycombinator.com/show"
    },
    {
      "url": "https://news.ycombinator.com/jobs"
    },
    {
      "url": "https://news.ycombinator.com"
    },
    {
      "url": "https://news.ycombinator.com/item?id=26566373"
    }
  ],
  "enableCommentHierarchy": false,
  "maxItems": 50,
  "endPage": 1,
  "extendOutputFunction": "($) => { return {} }",
  "proxy": {
    "useApifyProxy": true
  }
}
```

# 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 = {
    "startUrls": [
        {
            "url": "https://news.ycombinator.com/front"
        },
        {
            "url": "https://news.ycombinator.com/show"
        },
        {
            "url": "https://news.ycombinator.com/jobs"
        },
        {
            "url": "https://news.ycombinator.com"
        },
        {
            "url": "https://news.ycombinator.com/item?id=26566373"
        }
    ],
    "maxItems": 50,
    "endPage": 1,
    "extendOutputFunction": ($) => { return {} },
    "proxy": {
        "useApifyProxy": true
    }
};

// Run the Actor and wait for it to finish
const run = await client.actor("epctex/hackernews-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 = {
    "startUrls": [
        { "url": "https://news.ycombinator.com/front" },
        { "url": "https://news.ycombinator.com/show" },
        { "url": "https://news.ycombinator.com/jobs" },
        { "url": "https://news.ycombinator.com" },
        { "url": "https://news.ycombinator.com/item?id=26566373" },
    ],
    "maxItems": 50,
    "endPage": 1,
    "extendOutputFunction": "($) => { return {} }",
    "proxy": { "useApifyProxy": True },
}

# Run the Actor and wait for it to finish
run = client.actor("epctex/hackernews-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 '{
  "startUrls": [
    {
      "url": "https://news.ycombinator.com/front"
    },
    {
      "url": "https://news.ycombinator.com/show"
    },
    {
      "url": "https://news.ycombinator.com/jobs"
    },
    {
      "url": "https://news.ycombinator.com"
    },
    {
      "url": "https://news.ycombinator.com/item?id=26566373"
    }
  ],
  "maxItems": 50,
  "endPage": 1,
  "extendOutputFunction": "($) => { return {} }",
  "proxy": {
    "useApifyProxy": true
  }
}' |
apify call epctex/hackernews-scraper --silent --output-dataset

```

## MCP server setup

```json
{
    "mcpServers": {
        "apify": {
            "type": "http",
            "url": "https://mcp.apify.com/?tools=fetch-actor-details,epctex/hackernews-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/jv1G7XhUz1R34fV2L/builds/i8wGtuNjLgctEe44Z/openapi.json
