File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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 } " )
You can’t perform that action at this time.
0 commit comments