How to set the path to a browser executable with python webbrowser - python

I am trying to build a utility function to output beautiful soup code to a browser I have the following code:
def bs4_to_browser(bs4Tag):
import os
import webbrowser
html= str(bs4Tag)
# html = '<html> ... generated html string ...</html>'
path = os.path.abspath('temp.html')
url = 'file://' + path
with open(path, 'w') as f:
f.write(html)
webbrowser.open(url)
return
This works great and opens up the HTML in the default browser. However I would like to set the path to a portable firefox executable which is at:
F:\FirefoxPortable\firefox.exe
I am using win7. How to I set the path to the portable firefox executable?

You could start your portable Firefox directly with the url as an argument instead.
from subprocess import call
call(["F:\\FirefoxPortable\\firefox.exe", "-new-tab", url])

I know the question is old but here a code working with webbrowser and Python 3.11
myfirefox = webbrowser.Mozilla("F:\\FirefoxPortableESR\\FirefoxPortable.exe")
myfirefox.open(url)
As you will see, it works even if the .exe is not the "real" firefox.

Related

Download folder in python

I try download this zip. I used selenium and requests, but neither of them works and I don't know why.
Thank you for your advice.
from selenium import webdriver
import requests
url = 'http://vdp.cuzk.cz/vymenny_format/csv/20200131_OB_ADR_csv.zip'
driver = webdriver.Chrome('drivers\chromedriver.exe')
driver.get(url)
requests.get(url)
requests.get() downloads the entity into memory. This needs to be explicitly written to a file using open.
Example:
import requests
url = 'http://vdp.cuzk.cz/vymenny_format/csv/20200131_OB_ADR_csv.zip'
filename = 'c:/users/user/downloads/csv.zip'
filebody = requests.get(url)
open(filename, 'wb').write(filebody.content)
First of all, you don't need requests to download a file (in this case at least). As I don't know the errors you are getting, I would suggest double-checking the path to your chromedriver.exe and you should escape backslashes.
driver = webdriver.Chrome('drivers\\chromedriver.exe')
I tried your code (while entering the location of chromedriver on my computer) and it worked - I was able to download the file.

How to open an HTML file in the browser from Python?

I am trying to open an HTML file from Python but my script just displays the contents of the HTML file in Python instead of opening it in the browser. How can I fix this problem? How can I open the HTML file in my Chrome browser?
testdata.html
<div>
<img src="https://plot.ly/~user001/2.png" alt="Success vs Failure" style="max-width: 100%;width: 600px;" width="600" onerror="this.onerror=null;this.src='https://plot.ly/404.png';" />
<script data-plotly="user001:2" src="https://plot.ly/embed.js" async></script>
</div>
Python 2.7 script:
import urllib
page = urllib.urlopen('testdata.html').read()
print page
Try specifying the "file://" at the start of the URL.
// Also, use the absolute path of the file:
webbrowser.open('file://' + os.path.realpath(filename))
Or
import webbrowser
new = 2 # open in a new tab, if possible
// open a public URL, in this case, the webbrowser docs
url = "http://docs.python.org/library/webbrowser.html"
webbrowser.open(url,new=new)
// open an HTML file on my own (Windows) computer
url = "file://d/testdata.html"
webbrowser.open(url,new=new)
import os
os.system("start [your's_url]")
Enjoy!
You can use webbrowser library:
import webbrowser
url = 'file:///path/to/your/file/testdata.html'
webbrowser.open(url, new=2) # open in new tab
Here's a way that doesn't require external libraries and that can work of local files as well.
import subprocess
import os
url = "https://stackoverflow.com"
# or a file on your computer
# url = "/Users/yourusername/Desktop/index.html
try: # should work on Windows
os.startfile(url)
except AttributeError:
try: # should work on MacOS and most linux versions
subprocess.call(['open', url])
except:
print('Could not open URL')
You can use Selenium.
download the latest chromedriver, paste the chromedriver.exe in "C:\Python27\Scripts".
then
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("your page path")
print driver.page_source.encode('utf-8')
driver.quit()
display.stop()
I feel this is the easiest solution:
import os
os.getcwd() #To check the current working directory or path
os.chdir("D:\\Folder Name\\") # D:\Folder Name\ is the new path where you want to save the converted dataframe(df) to .html file
import webbrowser
df.to_html("filename.html") #Converting dataframe df to html and saving with a name 'filename' and
webbrowser.get("C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s").open("file://" + os.path.realpath("filename.html"))
you can download latest version of "gecodriver" from here.then add gecodriver executable file to your project.then pip install selenium and below the code for windows:
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
import os
#optional
options = Options()
options.set_preference('permissions.default.image', 2)
options.set_preference('dom.ipc.plugins.enabled.libflashplayer.so', False)
#for windows
Driver = webdriver.Firefox(options=options, executable_path='geckodriver.exe')
Driver.implicitly_wait(15)
#path of your project -> reference : "https://stackoverflow.com/questions/25389095/python-get-path-of-root-project-structure/40227116"
Root = os.path.dirname(os.path.abspath(__file__))
driver.get('file://' + Root + 'path/to/htmlfile')
Hope I Helped You:)
import os
os.system('open "/Applications/Safari.app" '+ '"' + os.path.realpath(fname)+ '"')

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')

webbrowser modules only opens url in mozilla?

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.

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