note: short code implementation environment: win10,python3
Code execution of this article
python opens the browser method 1:
By referencing the os package, calling the system method to call the system ie program to open the web address
The code is as follows:
import os os.system('"C:/Program Files/Internet Explorer/iexplore.exe" http://www.baidu.com')
python opens the browser method 2:
Open through the webrowser's open method:
-
python's webbrowser module supports some operations on the browser, mainly including the following three methods:
1.webbrowser.open(url, new=0, autoraise=True)
2.webbrowser.open_new(url)
3.webbrowser.open_new_tab(url)
webbrowser.open() method:
webbrowser.open(url, new=0, autoraise=True)
- Access the url address in the default browser of the system;
- If new = 0, the URL will open in the same browser window;
- If new=1, the new browser interface will be opened;
- new=2 the new browser tab will be opened.
webbrowser.get() method:
- You can get the action object of the system browser.
webbrowser.register() method:
- The browser type can be registered, and the allowed registered type names are as follows:
Type Name Class Name Notes 'mozilla' Mozilla('mozilla') 'firefox' Mozilla('mozilla') 'netscape' Mozilla('netscape') 'galeon' Galeon('galeon') 'epiphany' Galeon('epiphany') 'skipstone' BackgroundBrowser('skipstone') 'kfmclient' Konqueror() (1) 'konqueror' Konqueror() (1) 'kfm' Konqueror() (1) 'mosaic' BackgroundBrowser('mosaic') 'opera' Opera() 'grail' Grail() 'links' GenericBrowser('links') 'elinks' Elinks('elinks') 'lynx' GenericBrowser('lynx') 'w3m' GenericBrowser('w3m') 'windows-default' WindowsDefault (2) 'macosx' MacOSX('default') (3) 'safari' MacOSX('safari') (3) 'google-chrome' Chrome('google-chrome') 'chrome' Chrome('chrome') 'chromium' Chromium('chromium') 'chromium-browser' Chromium('chromium-browser')
eg: the code is as follows:
#-*- coding:UTF-8 -*- import sys import webbrowser sys.path.append("libs") url = 'https://translate.google.cn/' webbrowser.open(url) print(webbrowser.get())
- Use existing browser to open web page
- When calling other browsers, you need to register in advance, otherwise the default browser will open the page
import webbrowser IEPath = r'Own browser address' # For example, my: C:/Program Files/Internet Explorer/iexplore.exe webbrowser.register('IE', None, webbrowser.BackgroundBrowser(IEPath)) #The 'IE' here can be named by any other name, such as IE11. Save the browser you want to open to 'IE' webbrowser.get('IE').open('www.baidu.com',new=1,autoraise=True)