Home > Article > Backend Development > How to open a web page in python
1. Use the system’s own library os
The advantage of this method is that it can be used by any browser, but the disadvantage is that it cannot open web pages one after another freely.
import os os.system('"C:/Program Files/Internet Explorer/iexplore.exe" http://www.baidu.com')
2. Use the python integrated library webbroswer
Python’s webbrowser module supports some operations on the browser. There are mainly three methods:
import webbrowser webbrowser.open(url, new=0, autoraise=True) webbrowser.open_new(url) webbrowser.open_new_tab(url)
Although they are three methods, they are actually one method, but the parameters passed are different
webbrowser.open(url, new=0, autoraise=True) Access the url address in the system's default browser, if new =0, the url will be opened in the same
browser window; if new=1, a new browser window will be opened; new=2 a new browser tab will be opened
The other two functions webbrowser.open_new(url) and webbrowser.open_new_tab(url) actually return webbrowser.open() This method
It’s just that they don’t have the other two parameters.
You need to register in advance when calling other browsers, otherwise the default browser will open the page
import webbrowser chromePath = r'你的浏览器目录' # 例如我的:D:\Google\Chrome\Application\chrome.exe webbrowser.register('chrome', None, webbrowser.BackgroundBrowser(chromePath)) #这里的'chrome'可以用其它任意名字,如chrome111,这里将想打开的浏览器保存到'chrome' webbrowser.get('chrome').open('www.baidu.com',new=1,autoraise=True)
webbrowser The browser types supported by this library lock 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')
For more Python-related technical articles, please visit the Python Tutorial column to learn!
The above is the detailed content of How to open a web page in python. For more information, please follow other related articles on the PHP Chinese website!