Script to fetch quotes

It is a simple script to fetch quotes from the internet.
This commit is contained in:
nebula 2022-10-13 19:00:41 +05:45
parent 12ff44835f
commit 03da672e2a
2 changed files with 40 additions and 0 deletions

View File

@ -0,0 +1,5 @@
## A program to fetch random quote or term related quote from internet
use following command to search quote related to a specific term or phrase
python scrape.py -q your_search_term -r yes

View File

@ -0,0 +1,35 @@
#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}")