標籤:ted url when 命令列 pop can efi sed mil
非常多人,一提到Python,想到的就是爬蟲。我會一步一步的教你怎樣爬出某個網站。
今天就先介紹一下webbrowser,這個詞您肯定不會陌生。對,就是瀏覽器。
看看Python中對webbrowser的描寫敘述:
The webbrowser module provides a high-level interface to allow displaying Web-based documents to users. Under most circumstances, simply calling the open() function from this module will do the right thing.
以下就是對webbrowser的簡單有用了:
首先當然是匯入webbrowser模組了:
import webbrowser
可是這個時候等等。我有話要說。
在C++中,假設一個變數的名稱太長,我們往往有用typedef進行縮寫。Python中。相同能夠,比方我們嫌webbrowser太長了。希望用web替代,則能夠這麼匯入:
import webbrowser as web
接下來就介紹一些函數了:
-
webbrowser.open(url, new=0, autoraise=True)
-
Display url using the default browser. If new is 0, the url is opened in the same browser window if possible. If new is 1, a new browser window is opened if possible. If new is 2, a new browser page (“tab”) is opened if possible. If autoraise is True, the window is raised if possible (note that under many window managers this will occur regardless of the setting of this variable).
Note that on some platforms, trying to open a filename using this function, may work and start the operating system’s associated program. However, this is neither supported nor portable.
Changed in version 2.5: new can now be 2.
-
webbrowser.open_new(url)
-
Open url in a new window of the default browser, if possible, otherwise, open url in the only browser window.
-
webbrowser.open_new_tab(url)
-
Open url in a new page (“tab”) of the default browser, if possible, otherwise equivalent to open_new().
New in version 2.5.
-
webbrowser.
get([
name])
-
Return a controller object for the browser type name. If name is empty, return a controller for a default browser appropriate to the caller’s environment.
-
webbrowser.
register(
name,
constructor[,
instance])
-
Register the browser type name. Once a browser type is registered, the get() function can return a controller for that browser type. If instance is not provided, or is None, constructor will be called without parameters to create an instance when needed. If instance is provided, constructor will never be called, and may be None.
-
-
上面的都是官方的英文描寫敘述,單詞都非常easy。假設看不懂,勸你還是別編程了。
-
-
以下是幾個應用執行個體:
-
1用指定的瀏覽器來載入url
import webbrowserb = webbrowser.get(‘chrome‘)b.open(‘http://blog.csdn.net/wangshubo1989‘)
2對照應用
import webbrowserurl = ‘http://blog.csdn.net/wangshubo1989‘
# 預設瀏覽器開啟webbrowser.open_new(url) # opens in default browser# 使用 safari 開啟webbrowser.get(‘safari‘).open_new(url)# 在瀏覽器中用新標籤開啟webbrowser.open_new_tab(url) # opens in default browser# 在Safari中建立標籤並開啟urlwebbrowser.get(‘safari‘).open_new_tab(url)
關閉瀏覽器
對了,忘了寫怎樣關閉瀏覽器了
運行命令列就可以:
import os
os.system(‘taskkill /F /IM chrome.exe‘)
Python基礎--webbrowser