added formatting and fetching by keywords
This commit is contained in:
parent
50a4df4e71
commit
46dd6c0332
@ -9,6 +9,7 @@ __license__ = "None"
|
||||
import sys
|
||||
from newsapi import NewsApiClient
|
||||
from pandas.io.json import json_normalize
|
||||
import json
|
||||
import pandas as pd
|
||||
|
||||
# Init
|
||||
@ -23,6 +24,20 @@ all_articles = newsapi.get_everything(q='bitcoin', sources='bbc-news,the-verge',
|
||||
# /v2/top-headlines/sources
|
||||
sources = newsapi.get_sources()
|
||||
|
||||
def get_top_news_by_keyword(keyword):
|
||||
top_headlines = newsapi.get_top_headlines(q=keyword, sources='bbc-news,the-verge,cnn', language='en')
|
||||
out_file = open("top_headline.json", "w")
|
||||
json.dump(top_headlines, out_file)
|
||||
return top_headlines
|
||||
|
||||
def format_article(article):
|
||||
sourcename = article["source"]["name"]
|
||||
headline = article["title"]
|
||||
url = article["url"]
|
||||
formatted_article = f"<i>{sourcename}</i>\n{headline}\n\n<a href=\"{url}\">text</a>"
|
||||
|
||||
return formatted_article
|
||||
|
||||
if __name__ == '__main__':
|
||||
print(top_headlines)
|
||||
print(all_articles)
|
||||
articles = get_top_news_by_keyword("business")
|
||||
formatted_article = format_article(articles["articles"][0])
|
Loading…
Reference in New Issue
Block a user