File tree Expand file tree Collapse file tree 2 files changed +50
-0
lines changed Expand file tree Collapse file tree 2 files changed +50
-0
lines changed Original file line number Diff line number Diff line change
1
+ #Lab09: Quotes API
2
+
3
+ import requests
4
+ import pprint
5
+
6
+ response = requests .get ("https://favqs.com/api/qotd" , headers = {'Accept' : 'application/json' })
7
+ data = response .json ()
8
+
9
+ pprint .pprint (data )
10
+ # print(type(data))
11
+
12
+ # print(f"\nAuthor: {data['quote']['author']}")
13
+ # print(f"\nQuote: {data['quote']['body']}")
14
+
15
+ quotes = {
16
+ 'Author' : data ['quote' ]['author' ],
17
+ 'Quote' : data ['quote' ]['body' ]
18
+ }
19
+
20
+ print (f"\n Author: { quotes ['Author' ]} " )
21
+ print (f"\n Quote: { quotes ['Quote' ]} " )
Original file line number Diff line number Diff line change
1
+ import requests
2
+ import pprint
3
+
4
+ def print_results (p_num , search_term ):
5
+ response = requests .get (f"https://favqs.com/api/quotes?page=<page>&filter={ search_term } " , params = {'page' :{p_num }},
6
+ headers = {'Accept' : 'application/json' , 'Authorization' : 'Token token="855df50978dc9afd6bf86579913c9f8b"' })
7
+ data = response .json ()
8
+
9
+ for i in range (len (data ['quotes' ])):
10
+ print (f"\n Author: { data ['quotes' ][i ]['author' ]} " )
11
+ print (f"\n Quote: { data ['quotes' ][i ]['body' ]} " )
12
+
13
+ search_term = input ("enter a keyword to search for quotes: " )
14
+ p_num = 1
15
+
16
+ # search_term and p_num are defined outside the function so need to be included in the function(here)
17
+
18
+ while True :
19
+ print_results (p_num , search_term )
20
+ cont = input ("enter 'next page' or 'done' " )
21
+ if cont == 'done' :
22
+ print ("Goodbye" )
23
+ break
24
+ else :
25
+ p_num += 1
26
+
27
+
28
+ # pprint.pprint(data)
29
+ # print(f"\nAuthor: {data['quotes'][0]['author']}")
You can’t perform that action at this time.
0 commit comments