mirror of
https://github.com/metafy-social/python-scripts.git
synced 2024-11-23 20:11:10 +00:00
Script to fetch quotes
It is a simple script to fetch quotes from the internet.
This commit is contained in:
parent
12ff44835f
commit
03da672e2a
5
scripts/Quotes Fetch/readme.md
Normal file
5
scripts/Quotes Fetch/readme.md
Normal 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
|
35
scripts/Quotes Fetch/scrape.py
Normal file
35
scripts/Quotes Fetch/scrape.py
Normal 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}")
|
Loading…
Reference in New Issue
Block a user