Skip to content

Instantly share code, notes, and snippets.

@JKirchartz
Last active January 23, 2018 05:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save JKirchartz/05b1132a1151bb497bb408fdf4d0cc56 to your computer and use it in GitHub Desktop.
Save JKirchartz/05b1132a1151bb497bb408fdf4d0cc56 to your computer and use it in GitHub Desktop.
Download all quotes from BrainyQuotes by search terms, print in fortune format (note: doesn't seem to work on OSX)
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyleft (ↄ) 2016 jkirchartz <jkirchartz@Joels-iMac.local>
#
# Distributed under terms of the NPL (Necessary Public License) license.
"""
Download all quotes from BrainyQuotes by search terms, print in fortune format
usage:
python brainyquotes.py technology > technology
"""
from pyquery import PyQuery
import sys, random
def grabber(terms, i=1):
base_url = "https://www.brainyquote.com/search_results.html?q=" + terms
url = base_url + "&pg=" + str(i)
page = PyQuery(url)
quotes = page(".bqQt")
for quote in quotes.items():
txt = quote.find('.bqQuoteLink a').text()
author = quote.find('.bq-aut a').text()
print txt
print ' -- ' + author
print '%'
pagination = page('.pagination li:last').eq(1)
if pagination and not pagination.hasClass('disabled'):
i = i + 1
grabber(terms, i)
if __name__ == "__main__":
grabber('+'.join(sys.argv[1:]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment