Skip to content

Instantly share code, notes, and snippets.

@surajsahani
Created December 3, 2017 18:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save surajsahani/3a305e736cbf2675238e3a1c25c4591d to your computer and use it in GitHub Desktop.
Save surajsahani/3a305e736cbf2675238e3a1c25c4591d to your computer and use it in GitHub Desktop.
News In python
#!/usr/bin/python3
import json as js
import urllib.request
choice=1
def grabNews(source,sort,apikey):
url=' https://newsapi.org/v1/articles?'+'source='+source+'&sortBy='+sort+'&apikey='+apikey
# print(url)
data=urllib.request.urlopen(url).read().decode('utf-8')
newsdata = js.loads(data)
return newsdata
def grabSources(language):
url=' https://newsapi.org/v1/sources?language='+language
rawSource=urllib.request.urlopen(url).read().decode('utf-8')
allsources=js.loads(rawSource)
sourcesEnlist = [x['id'] for x in allsources['sources']]
return sourcesEnlist
while choice !='0':
temp = 0
sources=grabSources('en')
print('select a news source you like[using number]')
for item in sources:
print(temp,'->',item)
temp=temp+1
sltsource=int(input('enter your choice:'))
newsdata=grabNews(sources[sltsource],'top','PASTE YOUR API KEY HERE')
print('--------------------------------------------------------------------------------------')
for news in newsdata['articles']:
print(str(news['title']).upper())
print(news['description'])
print('url-> ',news['url'])
print('--------------------------------------------------------------------------------------')
print(' ')
choice=str(input('enter "0" to leave news'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment