Example of using python to call a browser and open a URL

Source: Internet
Author: User

Python opens a browser and can be used as a simple small program for web page refreshing. For learning purposes only.

The python webbrowser module supports operations on browsers. The following three methods are used:
Copy codeThe Code is as follows:
Webbrowser. open (url, new = 0, autoraise = True)
Webbrowser. open_new (url)
Webbrowser. open_new_tab (url)

We need to understand the webbrowser. open () method:

Copy codeThe Code is as follows: webbrowser. open (url, new = 0, autoraise = True)

Access the url address in the system's default browser. If new = 0, the url will be in the same
Open in the browser window; if new = 1, the new browser window will be opened; new = 2
The new browser tab is opened.

The webbrowser. get () method can obtain the operation object of the System Browser.

The webbrowser. register () method can register the browser type, and the type names allowed to be registered are as follows:
Copy codeThe Code is as follows: Type Name Class Name Notes
'Mozilla 'mozilla ('mozilla ')
'Firefox 'mozilla ('mozilla ')
'Netscape 'Mozilla ('netscape ')
'Galeon ('galeon ')
'Iphone any' Galeon ('iphone any ')
'Skipstone 'backgroundbrostone ('skipstone ')
'Kfmclient' Konqueror () (1)
'Konqueror' konqueror () (1)
'K' Konqueror () (1)
'Mosaic 'backgroundbroic ('mosaic ')
'Opera 'opera ()
'Grail 'grail ()
'Link' GenericBrowser ('link ')
'Elastics' elinks ('elastics ')
'Lynx 'genericbrowser ('lynx ')
'W3m 'GenericBrowser ('w3m ')
'Windows-default' WindowsDefault (2)
'Macos x' macosx ('default') (3)
'Safari 'MacOSX ('safari') (3)
'Google-chrome 'Chrome ('Google-chrome ')
'Chrome 'chrome ('chrome ')
'Chromium 'chromium ('chromium ')
'Chromium-browser' chromium ('chromium-browser ')

DEMO:
Copy codeThe Code is as follows:
#! /Usr/bin/env python
#-*-Coding: UTF-8 -*-
Import sys
Import webbrowser
Sys. path. append ("libs ")

 

Url = 'HTTP: // www.baidu.com'
Webbrowser. open (url)
Print webbrowser. get ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.