How to open a link in Python. Working with WebBrowser and solving a problem with Internet Explorer

In the course of work on the uni curser, I came across a standard Python module - WebBrowser . Through this module, I wanted to implement the work of the voice assistant with the default browser, but everything did not go as smoothly as expected. Let's first tell you what this module is and how it generally works.



WebBrowser is a module built into Python that provides a high-level interface for viewing web documents.



To get started, import the module with the command:



import webbrowser
      
      





Now there is a choice of how to open the link. There are two chairs:



1. Write in one line:



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





For example:



 webbrowser.open('https://vk.com', new=2)
      
      





If new = 0, the URL opens, if possible, in the same browser window. If the variable new = 1, a new browser window opens, if possible. If new = 2, a new browser page (โ€œtabโ€) opens, if possible.



You can safely skip the autoraise value, because it opens the browser on top of all windows, and most modern browsers spit on this variable even if it is False.



2. Do not suffer from remembering the parameters new and write humanly:



 webbrowser.open_new(url)
      
      





This design opens the URL in a new default browser window, if possible, otherwise it opens the URL in a single browser window.



 webbrowser.open_new_tab(url)
      
      





In this case, the URL will open on a new browser page (โ€œtab") by default, if possible, otherwise equivalent to open_new ().



Suppose you do not need a default browser. There is a cool .get () command to select a browser



 webbrowser.get(using=None)
      
      





Roughly speaking, you simply indicate which browser you use.



For example, opening a new tab in Google Chrome:



 webbrowser.get(using='google-chrome').open_new_tab('https://vk.com')
      
      





Browser Name Table:

Type name Class name
'mozilla' Mozilla ('mozilla')
'firefox' Mozilla ('mozilla')
'netscape' Mozilla ('netscape')
'galeon' Galeon ('galeon')
'epiphany' ' Galeon ('epiphany')
'skipstone' BackgroundBrowser ('skipstone')
'kfmclient' Konqueror ()
'konqueror' ' Konqueror ()
'kfm' Konqueror ()
'mosaic' BackgroundBrowser ('mosaic')
'opera' Opera ()
'grail' Grail ()
'links' GenericBrowser ('links')
'elinks' Elinks ('elinks')
'lynx' GenericBrowser ('lynx')
'w3m' GenericBrowser ('w3m')
'windows-default' Windowsdefault
'macosx' MacOSX ('default')
'safari' MacOSX ('safari')
'google-chrome' Chrome ('google-chrome')
'chrome' ' Chrome ('chrome')
'chromium' ' Chromium ('chromium')
'chromium-browser' Chromium ('chromium-browser')


But it is not always possible to get by with .get () alone, and in this case the .register () function comes to the rescue, for example:



 import webbrowser webbrowser.register('Chrome', None, webbrowser.BackgroundBrowser('C:\Program Files (x86)\Google\Chrome\Application\chrome.exe')) webbrowser.get('Chrome').open_new_tab('vk.com')
      
      





We indicated the path to Google Chrome, named it, and now all links open only in it. I hope weโ€™ve sorted out the WebBrowser module a bit and now let's move on to my little problem.



Problem



As mentioned earlier, for the course project, I chose the creation of a voice assistant. I wanted to teach him to follow links and search for information in a search engine. Of course, it would be possible to โ€œstuffโ€ a lot of libraries for this, but basically I wanted to implement this through the standard WebBrowser module.



Since most modern browsers have a link input line and a search line the same thing, it would seem that you can simply transfer the request to the same place the link is sent to.



For example:



 import webbrowser webbrowser.open_new_tab('https://vk.com') webbrowser.open_new_tab('')
      
      





According to the logic of this code, two tabs should open:



  1. Website vk.com
  2. Search Engine Query - Apples


But in fact, two different browsers open. The link is opened by our default browser, and the request is made by Internet Explorer (although it costs Windows 10, IE still opens instead of Edge). How to deal with this? Real gurus can get into the WebBrowser module and fix it there, but we will soberly assess our chances and go with the flow.



Since we are allowed to open only links in the default browser, we will only open links.



Solution steps



  1. We do a search query in our search engine (Yandex, Google, etc., etc.)

  2. We get the link



  3. And, as many have already guessed, just insert our link without what comes after "text ="



     import webbrowser webbrowser.open_new_tab('https://vk.com') webbrowser.open_new_tab('https://yandex.ru/search/?lr=10735&text=')
          
          



  4. Now you need to add the query text itself either through "+" or through "% s"



    • Through "+"



       webbrowser.open_new_tab('https://yandex.ru/search/?lr=10735&text='+'')
            
            



    • Across "%"



       webbrowser.open_new_tab('https://yandex.ru/search/?lr=10735&text=%s'%'')
            
            







Now let's make the user enter a link or request, and the program itself understands which method to use (call the link or insert it into the request link).



To begin with, we understand that the link carries a domain (.ru, .com, etc.), in the request, as a rule, they donโ€™t put an end to (buy a car, a movie online, etc.), but in the link space.



Therefore, we will look for a period and a space in what the user entered. We can do this thanks to the re module, which is also natively built into Python. Python offers two different primitive operations based on regular expressions: match searches for a pattern at the beginning of a string, while search searches the entire string. We will use the search operation.



 import webbrowser import re call = input('   : ') if re.search(r'\.', call): webbrowser.open_new_tab('https://' + call) elif re.search(r'\ ', call): webbrowser.open_new_tab('https://yandex.ru/search/?text='+call) else: webbrowser.open_new_tab('https://yandex.ru/search/?text=' + call)
      
      





Iโ€™ll explain the code a bit.



The user enters a link or request text into the call variable.



 if re.search(r'\.', call): webbrowser.open_new_tab('https://' + call)
      
      





The first condition checks the call variable for a point inside it. The character '\' is required, otherwise the module does not understand that the character is a dot before it.



 elif re.search(r'\ ', call):
      
      





In this condition, everything is the same as in the first, but verification is already underway for a space. A space indicates that we have a search query.



 else: webbrowser.open_new_tab('https://yandex.ru/search/?text=' + call)
      
      





And else , in turn, assigns everything that the user wrote without spaces or periods in the search query.



Checking for a space is required; otherwise, WebBrowser opens Internet Explorer.



Thank you all for your attention! I hope this article is useful to someone.




All Articles