0

From Python 2.7 (Anaconda) on OS X, how do I open a URL in a web browser? The following syntax:

import urllib2
urllib2.urlopen("https://www.google.com/")

prints my running .py file to a browser window. I've tried many examples online with the same effect.

5
  • That code when run with Python will not put your running python source file to a browser window, no. It'll return a response object instead. Commented Jun 28, 2015 at 17:59
  • That's what it is doing, instead of opening the google in a browser. Commented Jun 28, 2015 at 18:01
  • This simply queries a request to the server at the given URL. Commented Jun 28, 2015 at 18:02
  • @anon0909: then your question is unclear. If you want to control your browser from Python, use the webbrowser module. Commented Jun 28, 2015 at 18:02
  • @MartijnPieters Reminds me of this question. People don't really understand what open means, huh? Commented Jun 28, 2015 at 18:06

1 Answer 1

2
webbrowser.open('https://www.google.com') # open a passed URL string using default browser
Sign up to request clarification or add additional context in comments.

6 Comments

This gives the same result.
the script that calls this functions appears in a new tab in my default browser. It does not open google.com.
Please post a screenshot of your code, console, and browser.
@anon0909: it sounds as if you associated .py with something to open in your browser.
@MartijnPieters Exactly, you can't click to run on Mac OS. Use Terminal.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.