Last active
June 28, 2021 03:10
-
-
Save AceofSpades5757/8409a720ea660cf86c6081dcff6af2c8 to your computer and use it in GitHub Desktop.
Open a list of URLs on Windows
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" 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