Opening Up a Web Browser with Python

Start by importing webbrowser:

1
importwebbrowser

Next, simply call the webbrowser.open(url,new=0,) function. The variable, url, stands for the link you would like python to open using your default web browser (this will need to be of type ‘string’). The second variable, new, represents the method the browser will attempt to use in order to open the url: 1) new=0 means the url is opened in the same browser window if possible, 2) new=1 means a new browser window is opened if possible, and 3) new=2 means a new browser page (“tab”) is opened if possible.

Here’s an example:

1
webbrowser.open(url='http://en.wikipedia.org/wiki/Main_Page',new=2)

Calling:

1
webbrowser.get()

with no arguments will display the default browser.

Lastly, if you seek to open a webpage using python through the command line, you can achieve this by:

1
python -m webbrowser -t "http://en.wikipedia.org/wiki/Main_Page"

The above ‘-t’ flag means to use a new tab, and can be switched for -n if you require a new browser window to be used instead. For more about the webbrowser module, see the documentation on their site.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值