get_HN_favorites.py

Je Sian Keith Herman July 15, 2020 Updated: March 11, 2024 #Python #Scripting #CSV #Data Extraction

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)