The webbrowser
module has tools to manage a webbrowser from Python.
webbrowser.open()
opens a new browser window at a url:
In [1]:
import webbrowser
webbrowser.open('https://automatetheboringstuff.com')
Out[1]:
This is the primary function of the webbrowser
module, but it can be used as part of a script to improve web scraping. selenium
is a more full featured web browser module.
In [3]:
import webbrowser, sys, pyperclip
sys.argv # Pass system arguments to program; mapit.py '870' 'Valencia' 'St.'
# Check if command line arguments were passed; useful if this existed as a .py in the path (run via mapit 'Some address')
# For Jupyter version, will just pass in arguments earlier in document
if len(sys.argv) > 1:
# Join individual arguments into one string: mapit.py '870' 'Valencia' 'St.' > mapit.py '870 Valencia St.'
address = ' '.join(sys.argv[1:])
# Skip the first argument (mapit.py), but join every slice from [1:] with ' '
else:
# Read the clipboard if no arguments found
address = pyperclip.paste()
# Example Google Map URLs
# Default: https://www.google.com/maps/place/870+Valencia+St,+San+Francisco,+CA+94110/@37.7589845,-122.4237899,17z/data=!3m1!4b1!4m2!3m1!1s0x808f7e3db2792a09:0x4fc69a2eea9fb3d3
# Test: https://www.google.com/maps/place/870+Valencia+St,+San+Francisco,+CA+94110/
# Test: https://www.google.com/maps/place/870 Valencia St
# This works, so just concatenate the default google maps url with a spaced address variable
webbrowser.open('https://www.google.com/maps/place' + address)
To run this is a script, we would need to the full path to python, followed by the script, followed by the address.
An easy way to skip this process is to create a shell script holding the script addresses, and passing arguments to it:
In [ ]:
#! usr/bin/env bash
#python3 mapit.py %*