Skip to main content
  1. Gists/

📋get_HN_favorites.py

··95 words·1 min·
Je Sian Keith Herman
Author
Je Sian Keith Herman
INTJ Chemical Engineer that likes iced tea, iced coffee, and ice cream in no particular order.

A Python script to get the Hacker News favorites for a user.

See original source and license.

#!/usr/bin/env python3

# Author: gabrielsroka
# Source: https://github.com/gabrielsroka/gabrielsroka.github.io/blob/master/getHNFavorites.py
# License: MIT License

import requests
import time
from bs4 import BeautifulSoup # pip install beautifulsoup4

username = input('username: ')

# session uses connection pooling, often resulting in faster execution.
session = requests.Session()

base = 'https://news.ycombinator.com/'
path = f'favorites?id={username}'
while path:
    r = session.get(base + path)
    s = BeautifulSoup(r.text, 'html.parser')
    for a in s.select('span.titleline a'):
        print(a.text, a['href'])
    more = s.select_one('a.morelink')
    path = more['href'] if more else None
    if path: time.sleep(0.850)
Reply by Email