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 ()