Python 3.5 urllib won't open webpage in browser - python

I tried following code in VS2015, Eclipse and Spyder:
import urllib.request
with urllib.request.urlopen('https://www.python.org/') as response:
html = response.read()
In call cases it won't open the webpage in the browser. I am not sure what is the problem. Debug won't help. In VS2015 the program exists with code 0 which I suppose means successful.

You are using wrong library for the job. urllib module provides functions to send http requests and capture the result in your program. It has nothing to do with a web browser. What you are looking for is the webbrowser module. Here is an example:
import webbrowser
webbrowser.open('http://google.com')
This will show the web page in your browser.

urllib
is a module that is used to send request to web pages and read its contents.
Where as:
webbrowser
is used to open the desired url.
It can used as follows:
import webbrowser
webbrowser.open('http://docs.python.org/lib/module-webbrowser.html')
which usually re-uses existing browser window.
To open in new window:
webbrowser.open_new('http://docs.python.org/lib/module-webbrowser.html')
To open in new tab:
webbrowser.open_new_tab('http://docs.python.org/lib/module-webbrowser.html')
To access via command line interface:
$ python -m webbrowser -t "http://www.python.org"
-n: open new window
-t: open new tab
Here is python documentation for webbrowser:
python 3.6
python 2.7

Related

Issues with webbrowser in online Python IDE

I am trying to use an online IDE called repl.it and use the webbrowser module to open a webpage. This is the code I use:
from webbrowser import *
webbrowser.open('https://reddit.com')
But the function returns False as indicated in the online console.
Does this possibly have to do with the fact that the program runs in a remote server? If so, does repl.it have a hook between the server and the client to be able to perform web browser manipulations?
Yev from Repl.it here, that would be returning false because webbrowser probably needs a browser to execute and, additionally, repl.it runs code on headless machines, meaning there is no gui to run browsers in

How do I change the browser used by the scrapy view command?

How do I change the browser used by the view(response) command in the scrapy shell? It defaults to safari on my machine but I'd like it to use chrome as the development tools in chrome are better.
As eLRuLL already mentioned, view(response) uses webbrowser to open the web page you downloaded. To change its behavior, you need to set a BROWSER environment variable.
You could do this by adding the following line at the end of your ~/.bashrc file:
export BROWSER=/usr/bin/firefox (if you would like firefox to be used).
I don't have Chrome installed, but by doing a fast search on Google, it seems its path is /usr/bin/google-chrome-stable; therefore, you could try export BROWSER=/usr/bin/google-chrome-stable instead. I didn't test it for Chrome though.
There is no current way to specify which browser to use to open the response, as it internally uses the webbrowser package. This package uses your default configured browser to open the current response.
You could always change the default browser to chrome on your system, that should make webbrowser use it.
This fixed it for me:
If you're on windows 10, find or create a random html-file on your system.
Right click the html-file
Open with
Choose another app
Select your browser (e.g Google Chrome) and check the box "Always use this app to open .html"
Now attempt to use view(response) in the Scrapy shell again and it should work.
Try this
import webbrowser
from scrapy.utils.response import open_in_browser
open_in_browser(response, _openfunc=webbrowser.get("/usr/bin/google-chrome").open)

Python: open "chrome://" urls?

I am trying to open the Chrome extension page ("chrome://extensions/") in Chrome as the last step of a local script (so I can finally reload an extension there).
Unfortunately I am failing with this. :-(
-> Python 2.7 for win 8.1 x64
import webbrowser
webbrowser.get().open("chrome://extensions/")
This with Chrome as standard browser only results in windows telling me not knowing how to handle this:
And when I call Chrome directly via...
webbrowser.get("C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s").open_new_tab("chrome://extensions/"))
... or ...
import subprocess
subprocess.Popen([r'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe', 'chrome://extensions/']).wait()
... or when I try to open the URL via windows run dialog ...
"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" "chrome://extensions/"
... it always just opens a new window with the New Tab page, while a HTTP(S) URL opens these calls correctly.
Has anyone an idea how to open this chrome-specific page?
I believe that by default accessing/passing chrome url's is disabled/sandboxed outside of chrome, but there are cli switches that can be passed to chrome that you can use to change this??
List of Chrome switches here
Extension code docs, this might help you if all you are trying to do is reload an extension. Instead of doing it through the chrome:// URI

Page not loading at localhost:8080

I'm trying to learn SimpleCV, and in it's tutorials it says to display an image in the browser, use
img.show(type="browser")
Whenever I do this, my browser leads me to localhost:8080, and the page will not load. How can I start a simple server in Python so that the page will load?
Try calling import webbrowser from the shell. If it fails you need to install the library (pip install or easy_install).

How to check in Linux if my python script opens an HTTP connection

I'm looking for some command/tool that will show me that my python script opens a connection with a server and receiving data.
here is an example python script if it's going to help:
import requests
r = requests.post('https://stream.twitter.com/1/statuses/filter.json',data={'track':'justinbieber'},auth=('user','pass'),headers={'Connection':'close'},stream=True)

Categories