Skip to content

Commit 03da672

Browse files
committed
Script to fetch quotes
It is a simple script to fetch quotes from the internet.
1 parent 12ff448 commit 03da672

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

scripts/Quotes Fetch/readme.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## A program to fetch random quote or term related quote from internet
2+
3+
use following command to search quote related to a specific term or phrase
4+
5+
python scrape.py -q your_search_term -r yes

scripts/Quotes Fetch/scrape.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#import section
2+
import requests
3+
from bs4 import BeautifulSoup as bs
4+
import argparse
5+
from random import choice
6+
7+
# parser object to read cli content
8+
parser = argparse.ArgumentParser()
9+
parser.add_argument("-q", help="for search term")
10+
parser.add_argument("-r", help="if you want one random quote")
11+
12+
args = parser.parse_args()
13+
14+
15+
search_term = args.q
16+
page = 1
17+
# URl definition
18+
url = f"https://www.brainyquote.com/search_results?q={search_term}&pg={page}"
19+
20+
# main program
21+
r = requests.get(url)
22+
soup = bs(r.content, "html.parser")
23+
anchor1 = soup.find_all('a', {"class": "b-qt"})
24+
anchor2 = soup.find_all('a', {"class": "bq-aut"})
25+
26+
27+
if args.r != None:
28+
quote = choice(
29+
[f"{quote.text.strip()} - {author.text.strip()}" for quote, author in zip(anchor1, anchor2)])
30+
print(quote)
31+
else:
32+
for i, (quote, author) in enumerate(zip(anchor1, anchor2)):
33+
quote = quote.find('div').text.strip()
34+
author = author.text.strip()
35+
print(f"[{i}] : {quote} - {author}")

0 commit comments

Comments
 (0)