newsv2.py 文件源码

python
阅读 17 收藏 0 点赞 0 评论 0

项目:Whats-in-the-news-GEEK 作者: aribis369 项目源码 文件源码
def query(sea):
    url = "https://newsapi.org/v2/top-headlines?q=" + sea + "&apiKey="+ api_key
    try:
        with urlopen(url) as httpob:
            decob = httpob.read().decode("utf-8")
            jsonob = json.loads(decob)
            news = jsonob["articles"]

    # if api key is Invalid an HTTPError will be thrown.
    except HTTPError as e:
        print("Invalid API")
        create_api_file(file_name)
        return console()

    # draws a border to seperate posts.
    draw_border()
    for n in news:
        print(Fore.RESET)
        temp = n["source"]
        print(Back.YELLOW + src2(temp["name"]) + Back.RESET)
        try:
            if n["author"] == "":
                print((Style.BRIGHT +"By: " + src2(temp["name"])) + Style.RESET_ALL)
            else:
                print((Style.BRIGHT +"By: " + n["author"]) + Style.RESET_ALL)
        except:
            print((Style.BRIGHT +"By: " + src2(temp["name"])) + Style.RESET_ALL)
        try:
            print(Back.RED + (Style.BRIGHT + n["title"] + Style.RESET_ALL)) 
            print(Fore.BLUE + n["description"] + Fore.RESET)
        except:
            print(Fore.RED + "SOME ERROR OCCURED!!!\n" + Fore.RESET)
        print(Back.BLUE +(Style.BRIGHT + "url: "+ n["url"]) + Style.RESET_ALL + Back.RESET)
        #Similar to author, sometimes the Publishing time is not provided. 
        #For those cases, there will be no publishing time put. So except case has been made.
        try:
            print(Fore.GREEN + "Published At: "+ n["publishedAt"] + Fore.RESET )
        except:
            draw_border()
            continue
        draw_border()
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号