mirror of
https://github.com/metafy-social/python-scripts.git
synced 2024-11-27 22:11:10 +00:00
03da672e2a
It is a simple script to fetch quotes from the internet.
36 lines
981 B
Python
36 lines
981 B
Python
#import section
|
|
import requests
|
|
from bs4 import BeautifulSoup as bs
|
|
import argparse
|
|
from random import choice
|
|
|
|
# parser object to read cli content
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("-q", help="for search term")
|
|
parser.add_argument("-r", help="if you want one random quote")
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
search_term = args.q
|
|
page = 1
|
|
# URl definition
|
|
url = f"https://www.brainyquote.com/search_results?q={search_term}&pg={page}"
|
|
|
|
# main program
|
|
r = requests.get(url)
|
|
soup = bs(r.content, "html.parser")
|
|
anchor1 = soup.find_all('a', {"class": "b-qt"})
|
|
anchor2 = soup.find_all('a', {"class": "bq-aut"})
|
|
|
|
|
|
if args.r != None:
|
|
quote = choice(
|
|
[f"{quote.text.strip()} - {author.text.strip()}" for quote, author in zip(anchor1, anchor2)])
|
|
print(quote)
|
|
else:
|
|
for i, (quote, author) in enumerate(zip(anchor1, anchor2)):
|
|
quote = quote.find('div').text.strip()
|
|
author = author.text.strip()
|
|
print(f"[{i}] : {quote} - {author}")
|