Created
March 23, 2021 05:30
-
-
Save New-dev0/d2fb3f4eaec2be67245025aa80ac3029 to your computer and use it in GitHub Desktop.
download/get random quotes from quotefancy.com
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# A script to get/download random quotes from | |
# QuoteFancy.com in form of text or images ... | |
import random | |
import urllib | |
from urllib import urlopen | |
from bs4 import BeautifulSoup as bs | |
def get_quote(type: str = "text", | |
download: bool = False): | |
''' | |
It can be used to get Random quotes from Quotefancy.com | |
''' | |
alltypes = ["image", "text", "img", "txt"] | |
if type not in alltypes: | |
raise Exception("UnSupported Type") | |
if download and type == "text": | |
raise Exception("Text Object cant be Downloaded.") | |
link = "https://quotefancy.com/" | |
content = urlopen(link) | |
cl = bs(content, "html.parser", | |
from_encoding="utf-8") | |
fi = cl.find_all("div", "gridblock-title") | |
randompage = random.choice(fi).findNext()["href"] | |
ga = urlopen(randompage) | |
ref = bs(ga, | |
"html.parser", | |
from_encoding="utf-8") | |
if type == "text": | |
hu = ref.find_all("a", "quote-a") | |
rt = random.choice(hu).text | |
return str(rt) | |
imgs = ref.find_all("img", "load-lazily") | |
img = random.choice(imgs)["data-original"] | |
name = img.split("/")[::-1][0] | |
if download: | |
urllib.request.urlretrieve(img, name) | |
return name | |
return img |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment