Open a web page directly with Python
In case you need to open a web page using Python in “Chrome”, you can use this code:
# Open a page with Chrome through Python import webbrowser chromedir= 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s' webbrowser.get(chromedir).open("http://pythonprogramming.altervista.org")
You have to change the path of Chrome (or any other browser you want) checking where it has been installed into your computer’s folders .
Example: use webbrowser to go into your admin page of word press
Use it as a bookmark
You could use this code to get access to your admin pages adding the link, simply like this
# Open a page with Chrome through Python import webbrowser chromedir= 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s' webbrowser.get(chromedir).open("https://pythonprogramming.altervista.org/wp-admin")
Get the code into a function to use it more abstractly
You the could think to make a nice function
import webbrowser def openweb(link): chromedir= 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s' webbrowser.get(chromedir).open(link) openweb("https://pythonprogramming.altervista.org/wp-admin")
Use different browsers
and, for future additional features in your code, you could also think to specify the browser, why not?
# Open a page with Chrome through Python import webbrowser def openweb(browser, link): if browser == "chrome": chromedir= 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s' webbrowser.get(chromedir).open(link) openweb("chrome", "https://pythonprogramming.altervista.org/wp-admin")
Open more pages at once with webbroser
When we open the browser, usually there are some sites we check first. For example, if you have some blogs, like me, you will want to check them to see statistics, add post and so on. Having said this, the next idea that comes to my mind is to open more pages, this could be a nice shortcut to justify using python to make this. I could do it going to the settings of chrome, but I like automation and I am more at my ease with python instead of going into chrome settings… and obviously is more flexible and… even fun.
# Open a page with Chrome through Python import webbrowser def openweb(browser="", sites=[]): if browser == "chrome": chromedir= 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s' for site in sites: webbrowser.get(chromedir).open(site) openweb("chrome", [ "https://pythonprogramming.altervista.org/wp-admin", "https://aziendaitalia.altervista.org/wp-admin" ])
Add a tkinter window to show you the sites opened
You could, then, add a window that pops up showing you the sites opened with the script.
# Open a page with Chrome through Python import tkinter as tk import webbrowser def openweb(browser="", sites=[]): if browser == "chrome": chromedir= 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s' for site in sites: webbrowser.get(chromedir).open(site) return sites sites = openweb("chrome", [ "https://pythonprogramming.altervista.org/wp-admin", "https://aziendaitalia.altervista.org/wp-admin" ]) root = tk.Tk() tk.Label(root, text = "Python opened this sites:\n================").pack() for s in sites: tk.Label(root, text = s).pack() root.mainloop()
If you want to make things fast, save (as web.py, for example) this file in the default folder of windows (the folder that windows open in the search bar on the bottom left of the desktop)
and from the search bar digit web.py, hit enter and you will have chrome opened with the sites in that list.
Here is the pop up window popping out
To be continued…
Last thing we could add to this code is the chance to choose wich site to open among a list with checkboxes.
Let’s see how you can make checkboxes in tkinter:
import tkinter as tk class Check: def __init__(self, master, site): self.var = tk.IntVar() self.site = site self = tk.Checkbutton( master, text=self.site, variable=self.var, command=self.get) self.pack() def get(self): for c in [c1,c2,c3]: print(c.var.get(), end= " ") print() master = tk.Tk() c1 = Check(master, "site 1") c2 = Check(master, "site 2") c3 = Check(master, "site 3") master = tk.mainloop()
Adapting the code to our program
Let’s add the sites to the checkbuttons above
import tkinter as tk class Check: def __init__(self, master, site): self.var = tk.IntVar() self.site = site self = tk.Checkbutton( master, text=self.site, variable=self.var, command=self.get) self.pack() def get(self): for check in c: print(check.var.get(), end= " ") print() sites = ["https://pythonprogramming.altervista.org/wp-admin", "https://aziendaitalia.altervista.org/wp-admin"] c = [] master = tk.Tk() for s in sites: c.append(Check(master, s)) master = tk.mainloop()
The program prints at console 1 or zero when you check or uncheck the buttons
This is just an example, a possible use of the checkbuttons in tkinter. Come back here to see if I added some new stuffs to this project to amplify its features.
Now lets copy the openweb function in this script
import webbrowser def openweb(browser, link): if browser == "chrome": chromedir= 'C:/Program Files' ' (x86)/Google/Chrome/Application/' 'chrome.exe %s' webbrowser.get(chromedir).open(link)
We need to add a button that will check the checkbuttons selected
The final script
Ok, now we made it. We can check the sites that we want to open checking the checkbuttons in the windows that pop ups. I added a link of this post so that I could find it easily while I was editing it without going through the word press web editor, menus and searches.
Here is the window:
The code
import tkinter as tk import webbrowser def openweb(browser=""): if browser == "chrome": chromedir= 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s' for site in checked: webbrowser.get(chromedir).open(site) class Check: def __init__(self, master, site): self.var = tk.IntVar() self.site = site self = tk.Checkbutton( master, text=self.site, variable=self.var, command=self.print_info).pack() def print_info(self): print(f"status of {self.site}: {self.var.get()}") if self.var.get() == 1: checked.append(self.site) else: checked.remove(self.site) sites = ["https://pythonprogramming.altervista.org/wp-admin/post.php?post=122&action=edit", "https://pythonprogramming.altervista.org/wp-admin", "https://aziendaitalia.altervista.org/wp-admin"] checked = [] def start(): c = [] master = tk.Tk() for s in sites: c.append(Check(master, s)) tk.Button(master, text="Launch browser", command = lambda: openweb("chrome")).pack() master = tk.mainloop() start()
If you come back here I will probably do a live coding of this script… soon.
For italians…
Aprire una pagina web con Chrome usando Python e webbrowser
Se avete bisogno di aprire un link in Chrome usando Python, potete farlo in vari modi. È possibile, ad esempio, farlo con il modulo webbrowser.
Per prima cosa importate il modulo, poi con le funzioni get (per trovare il percorso del programma Chrome nel vostro pc) e open (per aprire il link), otterrete il risultato voluto.
# Open a page with Chrome through Python import webbrowser chromedir= 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s' webbrowser.get(chromedir).open("http://pythonprogramming.altervista.org")