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?

More than 1 year has passed since last update.

【Python】自動で指定のURLを開くようにしてみた

Last updated at Posted at 2022-07-31

概要

pythonで指定のURLを自動で開く処理を作ってみました。

前提条件

Pythonが既にインストールされていること。

pyautoguiの導入

コンソールを開いて下記のコマンドを実行してpyautoguiをインストールする。

pip install pyautogui

指定のURLを自動で開く処理の実装

作成したプログラムは下記になります。

import webbrowser
import pyautogui

# ブラウザを閉じる関数
def close_window():
    x = 10
    y = 10
    pyautogui.click(x, y)
    pyautogui.keyDown('ctrl')
    pyautogui.press('2')
    pyautogui.press('w')
    pyautogui.keyUp('ctrl')


# 開くURLのリスト
url = ['https://www.google.com','https://www.youtube.com/']

# ブラウザの表示モード(0:開いているウィンドウで開く、1:新規ウィンドウで開く、2新しいタブで開く)
window_open_mode = 2

# 開くブラウザを指定
browser = webbrowser.get("'C:\Program Files\Google\Chrome\Application\chrome.exe' %s")

for i in url:
    browser.open(i ,window_open_mode, True)
    close_window()

■処理内容

  • webbrowser.get()について
    デフォルトで開くブラウザがEdgeであったため、開くブラウザをChromeにするために加えてます。
    (デフォルトで開くブラウザがChromeの場合は不要です)

  • close_window()について
    webbrowserにはブラウザを閉じる関数がないため開いたタブを閉じる処理を関数で作成してます。
    処理内容としては以下の内容になります。
    ・開いたブラウザの左上をクリックする
    ・「Ctrl」ボタンを押したままにする
    ・「2」ボタンを押して2番目のタブに遷移する
    ・「w」ボタンを押してタブを閉じる
    ・「Ctrl」ボタンを離す

実行結果

実行した結果、Chromeの2個目のタブにGoogleのホーム画面とyoutubeのホームがそれぞれ開いて閉じられました。

次回について

次回は開いたブラウザ画面のスクショを取得する処理を書いてみようと思います。

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?