4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Pythonを使ってブラウザでWebページを開く方法

Posted at

もっと便利に使いたい

仕事で使ういつものたくさんのサイト、クリックひとつで開けたら便利だと思いませんか?
Pythonを使えばブラウザでWebページを開くのを自動化することができます。

動作環境

  • Windows / Mac / Linux どれでも可
  • Python 3.7.9

webbrowser.open関数を使う

webbrowserモジュールは標準ライブラリに含まれいるので、インストールは不要。
スクリプトの最初でインポートすればOK。

import webbrowser

次に、特定のWebページを開くには、webbrowser.open関数を使います。

webbrowser.open(url)

めっちゃ簡単!
変数urlには開きたいページのURLを指定します。

具体的なサンプルコードはこちら。

import webbrowser
url = 'https://google.com'
webbrowser.open(url, new=0, autoraise=True)
  • 第1引数 url: WebサイトのURLを設定します。
  • 第2引数 new: 表示モードを設定。下表参照
new 動作
0 同じウインドウで開く
1 新しいウインドウで開く
2 新しいタブで開く
  • 第3引数 autoraise: Trueなら、可能であればウィンドウが前面に表示されます。

まとめ

今回はPythonを使ってブラウザでWebページを開く方法をご紹介しました。
これを使うことでいつも行っている動作を効率化できます。
例えば、エクセルに指定したいつも開くURLをまとめて開いたりできます。
お試しあれ。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?