Python Foundation--webbrowser

Source: Internet
Author: User

A lot of people, when it comes to Python, think of reptiles. I'll show you how to crawl out of a site step-by-step.


Introduce WebBrowser today, this word you certainly will not be unfamiliar. Yes, it's the browser.


Look at the descriptive narrative of WebBrowser in Python:

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 would do the right thing.


The following is a simple and useful way to WebBrowser:

First of all, of course, import the WebBrowser module:

Import WebBrowser


But this time, wait. I have something to say.

In C + +, assuming that a variable's name is too long, we often use typedef for abbreviations. In Python. The same can, for example, we are too webbrowser too long. If you want to use Web overrides, you can import this:


Import WebBrowser as Web


The following are some of the functions:

webbrowser. open Span class= "Sig-paren" > ( URL ,   new=0 ,   autoraise=true )

Display URL using the default browser. If new is 0, the URL was 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 True is, the window was raised if possible (note that under many Windows managers this would occur Reg Ardless 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 Asso Ciated program. However, this is neither supported nor portable.

Changed in version 2.5: New can now is 2.

webbrowser.open_new(url)

Open URL in a new window of the default browser, if possible, otherwise, open URLs in the only browse R 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.

Code class= "Descclassname" style= "padding:0px 1px; Font-size:0.95em; background-color:transparent; " >webbrowser. get ( [ name span class= "optional" style= "FONT-SIZE:1.3EM;" >]

return a controller object for the browser type  name . IF&NBSP name  is empty, return a controller for a default browser appropriate to the caller ' s environment.

webbrowser. regi Ster ( name constructor [,  instance )

get ()  function can return a controller for that browser type. if  instance  is not provided, or Is none ,   constructor  will is called without parameters to create an instance when needed. if  instance  is provided,  constructor  will never be called, and may be < Code class= "docutils literal" style= "padding:0px 1px; Font-size:0.95em; Background-color:rgb (236, 240, 243); " >none .


The above are all official English descriptive narratives, the words are very easy. If you do not understand, advise you still do not program.


Here are a few examples of applications:

1 loading URLs with the specified browser

Import Webbrowserb = Webbrowser.get (' Chrome ') b.open (' http://blog.csdn.net/wangshubo1989 ')


2 Control applications

Import Webbrowserurl = '
http://blog.csdn.net/wangshubo1989'
# Default browser open webbrowser.open_new (URL) # opens in default browser# use Safari to open webbrowser.get (' Safari '). Open_new (URL) # In the browser, open the Webbrowser.open_new_tab (URL) # opens in Default browser# new tab in Safari and open Urlwebbrowser.get (' Safari '). Open_ New_tab (URL)


Close Browser

By the way, forgot to write how to close the browser.

Run the command line to:

Import OS

Os.system (' taskkill/f/im chrome.exe ')


Python Foundation--webbrowser

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.