File tree Expand file tree Collapse file tree 1 file changed +32
-3
lines changed Expand file tree Collapse file tree 1 file changed +32
-3
lines changed Original file line number Diff line number Diff line change 1
1
import requests
2
2
3
3
def version1 ():
4
- response = requests .get ('https://favqs.com/api/qotd' , headers = {
5
- 'Accept' : 'application/json' })
4
+ response = requests .get ('https://favqs.com/api/qotd' , headers = {'Accept' : 'application/json' })
6
5
7
6
quote = response .json ()['quote' ]['body' ]
8
7
author = response .json ()['quote' ]['author' ]
9
8
10
9
return print (f'"{ quote } " - { author } ' )
11
10
11
+ def version2 ():
12
+ page = 1
13
+ index = 0
14
+ search_term = input ('Enter a keyword to search for quotes: ' )
15
+
16
+ while True :
17
+ response = requests .get (f'https://favqs.com/api/quotes?page={ page } &filter={ search_term } ' , headers = {
18
+ 'Accept' : 'application/json' ,
19
+ 'Authorization' : 'Token token="855df50978dc9afd6bf86579913c9f8b"' })
20
+ last_page = response .json ()['last_page' ]
21
+ quotes = response .json ()['quotes' ]
22
+ num_quotes = len (quotes )
23
+
24
+ if last_page == False :
25
+ print (f'{ num_quotes } quotes associated with { search_term } - page { page } ' )
26
+
27
+ for quote in quotes :
28
+ print (f"'{ response .json ()['quotes' ][index ]['body' ]} ' - { response .json ()['quotes' ][index ]['author' ]} " )
29
+ index += 1
30
+
31
+ answer = input ("Enter 'next page' or 'done': " )
32
+
33
+ if answer == 'done' :
34
+ return
35
+ elif answer == 'next page' :
36
+ page += 1
37
+ index = 0
38
+
39
+
12
40
def main ():
13
- version1 ()
41
+ #version1()
42
+ version2 ()
14
43
15
44
main ()
You can’t perform that action at this time.
0 commit comments