Advertisement
iama_alpaca

QuoteGet.py

Jul 3rd, 2017
599
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.69 KB | None | 0 0
  1. #! /usr/bin/python2
  2. import bs4, requests, sys, random, urllib, time
  3.  
  4. searchurl = "https://duckduckgo.com/html/?q="+' '.join(sys.argv[1:]).replace(" ","+")+'+site%3Awikiquote.org'
  5.  
  6. res = requests.get(searchurl)
  7. soup = bs4.BeautifulSoup(res.text, 'html.parser')
  8.  
  9. if "If this error persists, please let us know: ops@duckduckgo.com" in soup:
  10.     print("Sorry, sometimes duckduckgo doesn't want to work properly. Try running the script again.")
  11.     sys.exit()
  12.  
  13. results = soup.select('.result__url')
  14.  
  15. for i in results:
  16.     if "wikiquote.org" in i.text:
  17.         wikiurl = urllib.unquote(i['href']).replace("/l/?kh=-1&uddg=","")
  18.         break
  19.  
  20. try:
  21.     wikiurl
  22. except NameError:
  23.     print('No wikiquote articles found for "'+' '.join(sys.argv[1:])+'"')
  24.     sys.exit()
  25. else:
  26.     wiki = requests.get(wikiurl)
  27.     wikisoup = bs4.BeautifulSoup(wiki.text, 'html.parser')
  28.     if wikisoup.select_one('h2 > span[id="Seasons"]') is not None and wikisoup.select_one('h2 > span[id="Seasons"]').text == "Seasons":
  29.         seasons = wikisoup.select('dl dd a')
  30.         seasonlinks = []
  31.         for i in seasons:
  32.             try:
  33.                 seasonlinks.append('https://en.wikiquote.org'+i['href'])
  34.             except KeyError:
  35.                 pass
  36.         randseason = random.choice(seasonlinks)
  37.         wiki = requests.get(randseason)
  38.         wikisoup = bs4.BeautifulSoup(wiki.text, 'html.parser')
  39.         quotes = wikisoup.select('dl dd')
  40.         name = wikisoup.select_one('.firstHeading')
  41.         rquote = random.choice(quotes).text
  42.         while rquote.startswith('['):
  43.             rquote = random.choice(quotes).text
  44.         print(rquote)
  45.     else:
  46.         quotes = wikisoup.select('dl dd')
  47.         name = wikisoup.select_one('.firstHeading')
  48.         rquote = random.choice(quotes).text
  49.         while rquote.startswith('['):
  50.             rquote = random.choice(quotes).text
  51.         print(rquote)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement