らずぱい日記

定年したので日記でもかくか。って感じ

気になるニュースは?

Pythonで面白そうなライブラリがあったので、触ってみた。

「GoogleNews - PyPI」というやつで、Googleニュースの記事を気になるカテゴリー

で収集するのに持って来い!ってやつ。

使い方は、[GoogleNews · PyPI]で確認してや。

こんな感じで取得や。

# -*- coding: utf-8 -*-
# 主要ニュース取得
from GoogleNews import GoogleNews
 
#検索したいニュースをkensakuに設定
kensaku = ["Python","無料講座","windows11","BTC","XRP","ETH","tips","仮想通貨"]

for ke in kensaku:
   googlenews = GoogleNews(lang='ja', encode='utf-8', period='1d')
   print(f"-----------------{ke}-------------\n")
   googlenews.get_news(ke)
   googlenews.search(ke)
   print(f'ニュース数:{googlenews.total_count()}本')
   res = googlenews.results()
   for n in res:
      title = n.get("title")
      link = n.get("link")
      if link.rfind("https://") == -1:
        link = "https://" + link
      else:
        pass
      print(f"■{title}\n{str(link)}\n")
      googlenews.clear()