-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathBrainyQuotes.py
74 lines (62 loc) · 1.98 KB
/
BrainyQuotes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# Coded By Gowtham on 03/06/2020
# Coded Using Visual Studio Code
import requests
from bs4 import BeautifulSoup as bs
def getQuotes(query):
BASE_URL = "https://www.brainyquote.com/"
query = query.strip().replace(" ","-")
url = f"https://www.brainyquote.com/topics/{query}-quotes"
quotesDictionary = {
"success": True,
"data": []
}
try:
soup = bs(requests.get(url).content, "lxml")
except:
quotesDictionary["success"] = False
return quotesDictionary
divs = soup.findAll("div",class_="grid-item qb clearfix bqQt")
for div in divs:
try:
try:
image = BASE_URL + div.find("img", {"class": "bqphtgrid"}).get("data-img-url")
except:
image = BASE_URL + div.find("img", {"class": "bqphtgrid"}).get("src")
except:
image = ""
try:
quote = div.find("img", {"class": "bqphtgrid"}).get("alt")
except:
quote = ""
try:
quotelink = BASE_URL + \
div.find("a", {"title": "view quote"}).get("href")
except:
quotelink = ""
try:
author = "By " + div.find("a", {"title": "view author"}).text
except:
author = ""
try:
authorlink = BASE_URL + \
div.find("a", {"title": "view author"}).get("href")
except:
authorlink = ""
try:
tagsElement = div.findAll(
"a", {"class": "qkw-btn btn btn-xs oncl_klc"})
except:
tagsElement = []
tags = []
for tag in tagsElement:
tags += tag
QuoteContent = {
"quote": quote,
"quotelink": quotelink,
"author": author,
"authorlink": authorlink,
"tags": tags,
"quoteImage":image
}
quotesDictionary["data"].append(QuoteContent)
return quotesDictionary