Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/python2
- import bs4, requests, sys, random, urllib, time
- searchurl = "https://duckduckgo.com/html/?q="+' '.join(sys.argv[1:]).replace(" ","+")+'+site%3Awikiquote.org'
- res = requests.get(searchurl)
- soup = bs4.BeautifulSoup(res.text, 'html.parser')
- if "If this error persists, please let us know: ops@duckduckgo.com" in soup:
- print("Sorry, sometimes duckduckgo doesn't want to work properly. Try running the script again.")
- sys.exit()
- results = soup.select('.result__url')
- for i in results:
- if "wikiquote.org" in i.text:
- wikiurl = urllib.unquote(i['href']).replace("/l/?kh=-1&uddg=","")
- break
- try:
- wikiurl
- except NameError:
- print('No wikiquote articles found for "'+' '.join(sys.argv[1:])+'"')
- sys.exit()
- else:
- wiki = requests.get(wikiurl)
- wikisoup = bs4.BeautifulSoup(wiki.text, 'html.parser')
- if wikisoup.select_one('h2 > span[id="Seasons"]') is not None and wikisoup.select_one('h2 > span[id="Seasons"]').text == "Seasons":
- seasons = wikisoup.select('dl dd a')
- seasonlinks = []
- for i in seasons:
- try:
- seasonlinks.append('https://en.wikiquote.org'+i['href'])
- except KeyError:
- pass
- randseason = random.choice(seasonlinks)
- wiki = requests.get(randseason)
- wikisoup = bs4.BeautifulSoup(wiki.text, 'html.parser')
- quotes = wikisoup.select('dl dd')
- name = wikisoup.select_one('.firstHeading')
- rquote = random.choice(quotes).text
- while rquote.startswith('['):
- rquote = random.choice(quotes).text
- print(rquote)
- else:
- quotes = wikisoup.select('dl dd')
- name = wikisoup.select_one('.firstHeading')
- rquote = random.choice(quotes).text
- while rquote.startswith('['):
- rquote = random.choice(quotes).text
- print(rquote)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement