Skip to content

Instantly share code, notes, and snippets.

@AceofSpades5757
Last active June 28, 2021 03:10
Show Gist options
  • Save AceofSpades5757/8409a720ea660cf86c6081dcff6af2c8 to your computer and use it in GitHub Desktop.
Save AceofSpades5757/8409a720ea660cf86c6081dcff6af2c8 to your computer and use it in GitHub Desktop.
Open a list of URLs on Windows
""" Opens URLs in the default browser.
I use this a lot when I need to open a whole lot of URLs at the same time.
1. Takes in a hard-coded set of `items`.
2. Generates a safe URL from `item`.
3. Passes each item to `get_url` to get the URL to open.
4. Opens each URL with built-in `webbrowser` package, delayed by `delay`.
Inputs
------
items : iterable of str
delay : int, default=2
Seconds of delay between each item.
get_url : func
Returns URL (str) to open.
"""
import sys
import time
import webbrowser
from urllib.parse import quote_plus
def get_url(item):
return f'https://www.google.com/search?q={item}'
""" Config """
delay = 2 # seconds
items = [
'my search item!',
]
""" Confirmation """
if not input('Are you sure you want to open these URLs (y/N)? '
).lower().strip().startswith('y'):
sys.exit(0)
""" Open Browser and Web Pages """
for item in items:
url = get_url(quote_plus(item))
webbrowser.open(url)
time.sleep(delay)
print(f"""
Item: {item}
URL: {url}
""")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment