LoginSignup
2
3

More than 3 years have passed since last update.

【Python】ヤフーニュースの主要トピックを取得

Posted at

当記事の目的

Pythonでヤフーニュースの主要トピックを取得する。

Pythonがインストールされていることを確認。

> python -V
Python 3.8.2

モジュールをインストール

requestsとbeautifulsoup4

> pip install requests beautifulsoup4

ニュース取得スクリプトを作成

news.py
import requests
from bs4 import BeautifulSoup

html = requests.get('https://news.yahoo.co.jp')

yahoo = BeautifulSoup(html.content, "html.parser")

for title in yahoo.select(".topicsList_main li.topicsListItem a"):
    print(title.getText())

実行

> python news.py
中国公船が領海侵入 2日連続
岐阜が「出口戦略」5指標示す
調停中断 親「子に会えない」
看護師と家族に誹謗中傷 神戸
正恩氏 プーチン氏に祝電
ソウル 遊興施設を営業禁止に
リトル・リチャード氏 死去
室井佑月と米山隆一氏 結婚へ
2
3
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
2
3