7
4

More than 3 years have passed since last update.

PythonでNewsAPIの日本語記事を取得する方法

Last updated at Posted at 2021-03-25

NewsAPIで日本語記事を取得しようと思ったらうまくいかなかったのでメモ

上記のライブラリを使用する。

上のドキュメントを参考にすれば、

# /v2/top-headlines
top_headlines = newsapi.get_top_headlines(q='ビットコイン',
                                          language='ja',
                                          country='jp')

とかすれば取れそうだが、これだと取得できない。

どうやらlanguage項目に'ja'がないらしい。
というわけで、

# /v2/top-headlines
top_headlines = newsapi.get_top_headlines(q='ビットコイン',
                                          country='jp')

とかやってみたら取れるかと思ったらこれまた取れない…
ライブラリを覗いてみると、

newsapi_client.py
 def get_top_headlines(  # noqa: C901
        self, q=None, qintitle=None, sources=None, language="en", country=None, category=None, page_size=None, page=None
    ):

なんとlangugeのデフォルトの引数が"en"に設定されている。
なので明示的にNoneを渡してやれば動く。

# /v2/top-headlines
top_headlines = newsapi.get_top_headlines(q='ビットコイン',
                                          language=None,
                                          country='jp')

これで取得できた

7
4
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
7
4