webbrowser modules only opens url in mozilla? - python

import webbrowser
webbrowser.open(url)
I am using this to open url in browser. But it opens only in 'Mozilla' why?

Just look at the docs. It uses the default browser. Look at webbrowser.get() for instructions on using a different browser.

Related

im using the web browser import and i want to figure out if i can run a google link in incognito from webbrowser. is that possible?

I'm writing out a code that opens up all necessary tabs using the webbrowser module so that I don't have to open them all manually when I open my pc, but one of the links I want it to open in incognito mode (all the links are google links) is this possible? I don't want all the code to open on incognito mode I just want the Microsoft Teams URL to open in incognito. Thanks in advance.
my code:
import webbrowser
import time
webbrowser.open('https://www.youtube.com/')
time.sleep(0.3)
webbrowser.open('https://mail.google.com/')
time.sleep(0.3)
webbrowser.open('https://teams.microsoft.com')
The solution is to specify the browser executable with the appropriate command line arguments to start it in incognito mode. The exact flag may vary by browser. Firefox, for example, takes the -private-window flag to start in incognito mode.
import webbrowser
import time
firefox_path = 'C:/Program Files/Mozilla Firefox/firefox.exe'
incognito_browser = webbrowser.get(firefox_path + ' -private-window %s')
webbrowser.open('https://www.youtube.com/')
time.sleep(0.3)
webbrowser.open('https://mail.google.com/')
time.sleep(0.3)
incognito_browser.open('https://teams.microsoft.com')
# ...could use now incognito_browser to open additional sites as desired.
The '%s' is substituted with the URL during the call to open and must be present for get to recognize that a command line invocation is being supplied rather than a named browser instance, e.g. 'opera'.

Getting HTML file from a webpage that is already opened in a browser in python 3

I have been looking on the internet for an answer for this but so far I haven't found quite what I was looking for. So far I'm able to open a webpage via Python webbrowser, but what I want to know is how to download the HTML file from that webpage that Python has asked the browser (firefox in this case) to open. This is because there are certain webpages with sections that I can not fully access without a certain browser extension/addon (MetaMask), as they require to also log in from within that extension, which is done automatically if I open firefox normally or with the webbrowser module. This is why requesting the HTML with an URL directly from Python with code such as this doesn't work:
import requests
url = 'https://www.google.com/'
r = requests.get(url)
r.text
from urllib.request import urlopen
with urlopen(url) as f:
html = f.read()
The only solution I have got so far is to open the webpage with the webbrowser module and then use the pyautogui module, which I can use to make my PC automatically press Ctrl+S (firefox browser hotkeys to save the HTML file from the webpage I'm currently in) and then make it press enter.
import webbrowser
import pyautogui
import time
def get_html():
url='https://example.com/'
webbrowser.open_new(url) #Open webpage in default browser (firefox)
time.sleep(1.2)
pyautogui.hotkey('ctrl', 's')
time.sleep(1)
pyautogui.press('enter')
get_html()
However, I was wondering if there is a more sophisticated and efficient way that doesn't involve simulated key pressing with pyautogui.
Can you try the following:
import requests
url = 'https://www.google.com/'
r = requests.get(url)
with open('page.html', 'w') as outfile:
outfile.write(r.text)
If the above solution doesn't work, you can use selenium library to open a browser:
import time
from selenium import webdriver
driver = webdriver.Firefox()
driver.get(url)
time.sleep(2)
with open('page.html', 'w') as f:
f.write(driver.page_source)

python open webbrowser and get html

Now let me start off by saying that I know bs4, scrapy, selenium and so much more can do this but that isnt what I want for numerous reasons.
What I would like to do is open a webbrowser (chrome, ie, firefox) and extract the html from the page after loading the site in that web browser from what webbrowser.
import webbrowser
import time
class ScreenCapture:
url = 'https://www.google.com/'
webbrowser.get("C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s").open(url)
# get html from browser that is open

WebBrowser Library Python 2.7

Python Webbrowser library has the new varibale for its open where, by default, it will open up a file in the same browser window. Is there a way to open up the same file in the same TAB. Like whatever the current page that is open on the browser, redirect that page the specified url.
current code is:
import webbrowser
url = "http://www.google.com"
webbrowser.open(url)
but this opens in a new tab but I want it to open in my current tab. Thanks in advance.
It should be as easy as adding new=0 like this:
webbrowser.open(url, new=0)
Display url using the default browser. If new is 0, the url is opened in the same browser window if possible.
according to:
https://docs.python.org/2/library/webbrowser.html
It is not possible with the Webbrowser module.
It is possible with Selenium module though
http://selenium-python.readthedocs.org/
download the module: https://pypi.python.org/pypi/sst/0.2.4
In the code:
from sst.actions import *
go_to('url')

How to open a URL in python

import urllib
fun open():
return urllib.urlopen('http://example.com')
But when example.com opens it does not render CSS or JavaScript. How can I open the webpage in a web browser?
#error(404)
def error404(error):
return webbrowser.open('http://example.com')
I am using bottle. Giving me the error:
TypeError("'bool' object is not iterable",)
with the webbrowser module
import webbrowser
webbrowser.open('http://example.com') # Go to example.com
import webbrowser
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
webbrowser.open_new(url)
Open url in a new window of the default browser
webbrowser.open_new_tab(url)
Open url in a new page (“tab”) of the default browser
On Windows
import os
os.system("start \"\" https://example.com")
On macOS
import os
os.system("open \"\" https://example.com")
On Linux
import os
os.system("xdg-open \"\" https://example.com")
Cross-Platform
import webbrowser
webbrowser.open('https://example.com')
You have to read the data too.
Check out : http://www.doughellmann.com/PyMOTW/urllib2/ to understand it.
response = urllib2.urlopen(..)
headers = response.info()
data = response.read()
Of course, what you want is to render it in browser and aaronasterling's answer is what you want.
You could also try:
import os
os.system("start \"\" http://example.com")
This, other than #aaronasterling ´s answer has the advantage that it opens the default web browser.
Be sure not to forget the "http://".
Here is another way to do it.
import webbrowser
webbrowser.open("foobar.com")
I think this is the easy way to open a URL using this function
webbrowser.open_new_tab(url)

Categories