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

Source: Internet
Author: User
This article mainly introduces the example of using python to call a browser and open a website. it is implemented using the webbrowser module. if you need it, refer to python to open a browser, you can make simple small programs for web page refreshing. For learning purposes only.

The python webbrowser module supports operations on browsers. the following three methods are used:

The 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:

The 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:

The 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:

The 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.