-1

I have wrote the code which searches for particular keyword and give the news through news API but I have hard coded the q (Keywords or phrases to search for in the article title and body.) But I want it should be dynamic like user give a keyword to search and it provides everything. Can anyone please help me. Below is the code snippet which i am doing.

import requests
url = ('http://newsapi.org/v2/everything?'
       'q=Python&'
       'from=2020-08-17&'
       'sortBy=popularity&'
       'apiKey=xxxxxxx')

response = requests.get(url)
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437

1 Answers1

1

Hope this will solve your query,

import requests
What=input("Enter the q: ")
url = ('http://newsapi.org/v2/everything?'
       f'q={What}'
       'from=2020-08-17&'
       'sortBy=popularity&'
       'apiKey=xxxxxxx')

response = requests.get(url)

This code will ask user to enter q, store it in "What" variable and hence can be dynamically changed

  • Thanks @Nikhil I got the error with little twig in the solution you provided and it worked . Posting it for broader audience import requests What=input("Enter the q: ") url = ('http://newsapi.org/v2/everything?' 'q='+What+'&' 'from=2020-08-17&' 'sortBy=popularity&' 'apiKey=XXXXXXXXXX') – SHIBASHISH TRIPATHY Aug 17 '20 at 15:28