0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ニュースを取得してくれるプログラミング ChatGPT参考

0
Posted at

!pip install feedparser pandas

import feedparser
import pandas as pd
from datetime import datetime
import re

==========================================

ニュース速報まとめプログラム

Google Colab用

==========================================

ニュースRSS

RSS_URL = "https://news.google.com/rss?hl=ja&gl=JP&ceid=JP:ja"

print("ニュースを取得しています...")
print()

RSSを読み込む

feed = feedparser.parse(RSS_URL)

news_list = []

取得するニュース数

MAX_NEWS = 30

for entry in feed.entries[:MAX_NEWS]:

title = entry.get("title", "タイトルなし")
link = entry.get("link", "")
published = entry.get("published", "")

news_list.append({
    "タイトル": title,
    "公開日時": published,
    "URL": link
})

DataFrameにする

df = pd.DataFrame(news_list)

==========================================

速報キーワードをチェック

==========================================

breaking_words = [
"速報",
"緊急",
"地震",
"津波",
"警報",
"事故",
"火災",
"逮捕",
"大雨",
"台風",
"災害"
]

def check_breaking(title):

for word in breaking_words:
    if word in title:
        return "速報"

return "通常"

df["種類"] = df["タイトル"].apply(check_breaking)

==========================================

ニュースを表示

==========================================

print("=" * 60)
print(" 📰 ニュース速報まとめ")
print("=" * 60)

print("取得日時:", datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
print()

for i, row in df.iterrows():

if row["種類"] == "速報":
    mark = "🚨"
else:
    mark = "📰"

print(f"{mark} {i + 1}. {row['タイトル']}")
print(f"   公開日時:{row['公開日時']}")
print(f"   URL:{row['URL']}")
print()

==========================================

CSVとして保存

==========================================

df.to_csv(
"news_summary.csv",
index=False,
encoding="utf-8-sig"
)

print("=" * 60)
print("ニュースを news_summary.csv に保存しました。")

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?