본문 바로가기
Python

[Python] 파이썬 webbrowser URL 열기 크롬으로 변경

by Just Do Barro 2021. 2. 21.

 

필자의 환경설정

Python 3.9.1

문제 상황

Visual Studio Code에서 webbrowser 라이브러리를 import 하여 Url을 Open 시 Internet Explorer 브라우저로 오픈됨.

크롬 브라우저로 url 열기를 해보겠다.

해결

- webbrowser 라이브러리를 import 해야한다.

- 각 운영체제에 맞는 경로를 복사해야한다. 

[코드]

import webbrowser

url = 'http://docs.python.org/'

# MacOS
chrome_path = 'open -a /Applications/Google\ Chrome.app %s'

# Windows
# chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'

# Linux
# chrome_path = '/usr/bin/google-chrome %s'

webbrowser.get(chrome_path).open(url)

 

적용

자동으로 URL 경로를 따라 크롬 브라우저에 웹페이지 열기

아래 코드는 리스트에 url을 요소로 저장하여 자동으로 저장된 url을 4초마다 열게 하는 코드이다.

필자는 윈도우 운영체제를 쓰기 때문에 

chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'

을 사용하였다. 경로는 수정할 필요없이 복사하여 붙여넣기 하면 된다. 

[코드]

import webbrowser # 웹 브라우저를 여는 라이브러리 프로그램
import time

list1 = [

    "https://www.inflearn.com/",
    "https://opentutorials.org/",
    "https://www.naver.com/"

]
# 기본 브라우저 chrome으로 변경  WINDOW 기준 경로
chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'

for url in list1:
    webbrowser.get(chrome_path).open(url)
    time.sleep(4) # 4초 쉬기 4초마다 반복

 

참고 및 출처

stackoverflow.com/questions/22445217/python-webbrowser-open-to-open-chrome-browser

 

Python webbrowser.open() to open Chrome browser

According to the documentation http://docs.python.org/3.3/library/webbrowser.html it's supposed to open in the default browser, but for some reason on my machine it opens IE. I did a google search ...

stackoverflow.com

[입문자를 위한 파이썬 기초 따라잡기 강의]25편 블로그 조회수 올리기 프로그램 - YouTube

 

댓글