python模組介紹-webbrowser:方便的web瀏覽器控制器

來源:互聯網
上載者:User

標籤:


源碼:Lib/webbrowser.py

簡介

webbrowser模組提供了展示基於web文檔的高層介面。多數情況下, 簡單地調用open() 函數即可。

在Unix的X11下,首選調用圖形瀏覽器。如果圖形瀏覽器不可用或者沒有顯示終端,則使用文字模式瀏覽器 。如果使用文字模式瀏覽器, 在使用者退出瀏覽器之前調用進程都會阻塞。

如果設定了BROWSER環境變數,它將覆蓋平台預設的瀏覽器列表。 BROWSER是os.pathsep分割的的瀏覽器列表。如果列表的部分值包含字串“%s”,解析為瀏覽器命令列的參數。

對於非Unix平台或unix使用遠程瀏覽器時, 控制進程不會等待使用者完成瀏覽, 但允許遠程瀏覽器顯示自己的視窗 。如沒有遠程瀏覽器, 控制進程將發起新的瀏覽器並等待。

webbrowser支援命令列介面。 它接受URL作為參數。選擇性參數:“-n”在新的瀏覽視窗開啟URL;“-t”開啟新的tab。

$ python -m webbrowser -t "http://automationtesting.sinaapp.com/blog/url"[email protected]:~$ [4860:4860:0520/095513:ERROR:url_pattern_set.cc(240)] Invalid url pattern: chrome://print/*[4897:4897:0520/095513:ERROR:sandbox_linux.cc(343)] InitializeSandbox() called with multiple threads in process gpu-process[email protected]:~$ [email protected]:~$ python -m webbrowser -t "http://automationtesting.sinaapp.com/blog/url"[email protected]:~$ 已在現有的瀏覽器會話中建立新的視窗。[email protected]:~$
快速入門
import webbrowserwebbrowser.open(‘http://automationtesting.sinaapp.com‘)webbrowser.open_new_tab(‘http://automationtesting.sinaapp.com/blog/url‘)
上例會開啟兩個標籤。 介面介紹
  • 異常:exception webbrowser.Error: 當瀏覽器控制錯誤時拋出該異常。

函數
  • webbrowser.open(url, new=0, autoraise=True)

使用預設的瀏覽器訪問url。如果new等於0,則url會在盡量在已有瀏覽器視窗開啟。如果new等於1, 則開啟新的瀏覽器視窗。new等於2,盡量在瀏覽器開啟新標籤(tab)。如果autoraise為True,則瀏覽器視窗顯示在最前面(注意在多數視窗 管理器會這樣)。

注意在某些平台上,開啟檔案名稱的時候 ,可能會啟動系統的關聯程式。另外一些系統預設用最節省資源的方式開啟瀏覽器。

  • webbrowser.open_new(url)

使用預設的瀏覽器開啟url,盡量只有一個視窗

  • webbrowser.open_new_tab(url)

使用預設的瀏覽器的新tab中開啟url。

  • webbrowser.get([name])

返回制定瀏覽器控制器對象。如果name為空白,則返回預設瀏覽器的控制器。

  • webbrowser.register(name, constructor, instance=None)

註冊瀏覽器類型:name。註冊後get() 函數可以返回該瀏覽器類型的控制器 。如果instance沒有提供或者是None, 建構函式可以無參數調用並建立執行個體。如果有提供instance, 就不會調用建構函式。

This entry point is only useful if you plan to either set the BROWSER variable or call get() with a nonempty argument matching the name of a handler you declare.

僅僅在設定BROWSER變數或使用不允許為 NULL 的參數調用get()時有效。 你聲明一個處理常式的名稱相匹配的。

預定義的瀏覽器類型

參見:https://docs.python.org/2/library/webbrowser.html

瀏覽器控制器對象
  • controller.open(url, new=0, autoraise=True)

  • controller.open_new(url)

  • controller.open_new_tab(url)

請參考模組函數文檔。


參考資料
  • webbrowser英文文檔

  • 作者部落格:http://my.oschina.net/u/1433482


python模組介紹-webbrowser:方便的web瀏覽器控制器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.