Skip to content

Instantly share code, notes, and snippets.

@LevitatingBusinessMan
Created July 17, 2023 17:47
Show Gist options
  • Select an option

  • Save LevitatingBusinessMan/d95e8964a828809049814b66fce8fa0f to your computer and use it in GitHub Desktop.

Select an option

Save LevitatingBusinessMan/d95e8964a828809049814b66fce8fa0f to your computer and use it in GitHub Desktop.
Hacker News one-liner scraper. Run with `ruby -rnet/http -rnokogiri hn.rb`
File.write 'hn.csv', Nokogiri(Net::HTTP.get(URI('https://news.ycombinator.com'))).css('.titleline > a', '.subline').each_slice(30).to_a.transpose.map {|a,l|[a.text, a.attr('href'), l.css('.score').first.text.chomp(' points').to_i, l.css('.hnuser').first.text, l.css('.age').first.attr('title')].to_csv}.join
pp Nokogiri(Net::HTTP.get(URI('https://news.ycombinator.com'))).css('.titleline > a','.subline').each_slice(30).to_a.transpose.map{|a,l|[a.text,a.attr('href'),l.css('.score').first.text.chomp(' points').to_i,l.css('.hnuser').first.text, l.css('.age').first.attr('title')]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment