forked from geekcomputers/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
9 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,15 @@ | ||
# Created by sarathkaul on 11/11/19 | ||
|
||
import requests | ||
|
||
_NEWS_API = "https://newsapi.org/v1/articles?source=bbc-news&sortBy=top&apiKey=4dbc17e007ab436fb66416009dfb59a8" | ||
|
||
|
||
def fetch_bbc_news(): | ||
# fetching data in json format | ||
bbc_page_fetch = requests.get(_NEWS_API).json() | ||
|
||
# getting all articles in a string article | ||
article = bbc_page_fetch["articles"] | ||
|
||
# result containing all trending news | ||
results = [] | ||
_NEWS_API = "https://newsapi.org/v1/articles?source=bbc-news&sortBy=top&apiKey=" | ||
|
||
for a_article in article: | ||
results.append(a_article) | ||
|
||
for a_result in range(len(results)): | ||
# printing all trending news | ||
print str(a_result + 1) + ".) ", results[a_result]['title'] | ||
def fetch_bbc_news(bbc_news_api_key: str) -> None: | ||
# fetching a list of articles in json format | ||
bbc_news_page = requests.get(_NEWS_API + bbc_news_api_key).json() | ||
# each article in the list is a dict | ||
for news, article in enumerate(bbc_news_page["articles"], 1): | ||
print(f"{news}.) {article['title']}") | ||
|
||
|
||
if __name__ == '__main__': | ||
fetch_bbc_news() | ||
if __name__ == "__main__": | ||
fetch_bbc_news(bbc_news_api_key="<Your BBC News API key goes here>") |