1
- """
2
- This module will take an input string and print the search result.
3
- """
4
- import requests
5
-
1
+ import requests # This module will help extract data
6
2
7
3
def main ():
8
4
@@ -11,26 +7,28 @@ def main():
11
7
print ('\n \n ' )
12
8
search_for = input ('search for: ' ) # Input string to search
13
9
14
- response = requests .get (f'http://api.duckduckgo.com/?q={ search_for } &format=json' ) # Will send request with search_for variable
10
+ response = requests .get (f'http://api.duckduckgo.com/?q={ search_for } &format=json' ) # Send request with { search_for} variable
15
11
response = response .json () # The result will be in json format
12
+ # it will be in key, value pair which will be easy to find answers
16
13
17
- # Printing search result here
18
- print ('Heading:' ,response ['Heading' ])
19
- print ('\n Abstract:' ,response ['Abstract' ])
14
+ # Printing search result here we will print the values of keys inside brackets
15
+ print ('Heading:' ,response ['Heading' ])
16
+ print ('\n Abstract:' ,response ['Abstract' ])
20
17
print ('\n Abstract source:' ,response ['AbstractSource' ])
21
- print ('\n Abstract text:' ,response ['AbstractText' ])
22
- print ('\n Definition:' ,response ['Definition' ])
18
+ print ('\n Abstract text:' ,response ['AbstractText' ])
19
+ print ('\n Definition:' ,response ['Definition' ])
23
20
print ('Related Topic:' )
24
21
25
- related_no = 2 # Will show only no of text in realted topics
26
- for i in response ['RelatedTopics' ]: # Will loop through Text in RelatedTopics
22
+ related_no = 2 # Show only no of text in realted topics
23
+ # Sometimes tere are less related topics so keep related no low
24
+ for i in response ['RelatedTopics' ]: # Loop through Text in RelatedTopics
27
25
if related_no <= 0 :
28
26
break
29
27
print (i ['Text' ])
30
28
related_no = related_no - 1
31
29
32
-
33
30
except KeyboardInterrupt : # ctrl+c will call KeyboardInterrupt and while loop will stop
31
+ # it will stop execution of code and will print Exiting..
34
32
print ('\n Exiting..' )
35
33
break
36
34
0 commit comments