• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / API / How to use the Hacker News API

How to use the Hacker News API

Author: PFB Staff Writer
Last Updated: August 28, 2020

Hacker News API – Overview

Today I will go through the “Unofficial Python API for Hacker News”, which can be found here

What is Hacker News?

Hacker News is a social news website that caters to programmers and entrepreneurs, delivering content related to computer science and entrepreneurship. [source]

Python Wrapper

In the How to use Reddit API in Python, we described how to directly access Reddit API directly. Another way of doing that would be to use one of the Reddit wrappers. A wrapper is an API client, that are commonly used to wrap the API into easy to use functions by doing the API calls itself. When using a wrapper, you don’t have to care what’s going on behind the scenes, which sometimes can be easier for a beginner. Think of it as a interface between python and a web service.

Getting started

Let’s get started and install it by using the pip tool.

pip search HackerNews
HackerNews                - Python API for Hacker News.

pip install HackerNews

Successfully installed HackerNews Cleaning up… pip show HackerNews — Name: HackerNews Version: 1.3.3 Location: /usr/local/lib/python2.7/dist-packages Requires: BeautifulSoup4

API documentation

It is suggested that you read the documentation, which is available on this Github page. The API contains a few classes (HN and Story). The classes provides you with methods. The available methods for the HN class are: get_top_stories() Returns a list of Story objects from the homepage of HN get_newest_stories() Returns a list of Story objects from the newest page of HN The available method for the Story class is: print_story() – Print the details of a story

Run the program

The author of the API provides an example on his Github page. The example prints top and new posts from Hacker news. Open your favorite editor and copy and paste the following code.

#!/usr/bin/env python

from hn import HN

hn = HN()

# print top 10 stories from homepage
for story in hn.get_top_stories()[:10]:
    story.print_story()
    print '*' * 50
    print ''

# print 10 latest stories
for story in hn.get_newest_stories()[:10]:
    story.print_story()
    print '*' * 50
    print ''

# print all self posts from the homepage
for story in hn.get_top_stories():
    if story.is_self_post:
        story.print_story()
        print '*' * 50
        print ''

Save it as test_bot.py and run it. The program will loop through every story (post) on hacker news and give you the top 10 latest stories. For every post it will show information seen below Story details rank – the rank of story on the page story_id – the story’s id title – the title of the story is_self_post – true for self/job stories link – the url it points to (None for self posts) domain – the domain of the link (None for self posts) points – the points/karma on the story submitter – the user who submitted the story submitter_link – the above user profile link published_time – the published time ago num_comments – the number of comments it has comments_link – the link to the comments page Using a python wrapper for an API is nice and simple, but try to understand whats going on behind the scenes. It is important that you understand what is going on in the code, and once you learned it, you can go for wrapper. Try to get knowledge about Json and API’s, to understand how most of them work.

More Reading

HackerNewsAPI

List of Python APIs

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: API, Hackernews Author: PFB Staff Writer

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us