How to open a URL in python - 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)

Related

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)

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 set the path to a browser executable with python webbrowser

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.

Python - Load a webpage in hidden

I want to load a webpage in python and do something
import webbrowser
url = 'http://google.com'
webbrowser.open(url)
//do something
But I don't want the browser has been showed on my screen, I want to hide it so that I can do some other stuff,
please give me some advices,
thanks
In some cases, autoraise is working.
Example:
import webbrowser
webbrowser.open('www.yoursite.com', autoraise=False)

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.

Categories