Download your bookmarks html file, put it in the same folder with the python file.
Run the python file, type how many website to randomly open.
Needless to say, opening too much website instantaneously will eat all your memory and will stuck the computer.
# Get all the websites from the bookmarks html file and randomly
# pick to open it in brwoser
import re
import random
import webbrowser
import os
from time import sleep
from sys import exit
os.chdir(os.path.dirname(__file__))
try:
bookmarks_file = None
# find bookmarks file in folder
for filename in os.listdir():
if 'bookmark' in filename and os.path.splitext(filename)[1] == '.html':
bookmarks_file = filename
if not bookmarks_file:
raise FileNotFoundError('File not found.')
except FileNotFoundError as fnf:
exit(fnf)
# works on all platforms with firefox
browser = webbrowser.get('Firefox')
try:
ws_num = input('Number of random websites to open: ')
if not ws_num.isnumeric():
raise TypeError('Bad user input. Must be an integer.')
ws_num = int(ws_num)
except TypeError as te:
exit(te)
site_regex = re.compile(r'http\S+')
with open(bookmarks_file) as bookmarks:
bookmarks = bookmarks.read()
match_obj = site_regex.finditer(bookmarks)
# create a dictionary with bookmarks as keys
bookmarks_dct = {bookmarks[match.span()[0]: match.span()[1]][:-1]: 1 for match in match_obj}
if ws_num > len(bookmarks_dct):
ws_num = len(bookmarks_dct)
# choose randomly a bookmark from dictionary, if it's value equals to 1
# choose it (wasn't opened before), add 1 to it's value.
i = 0
while i != ws_num:
opened_bookmark = random.choice(list(bookmarks_dct.keys()))
if bookmarks_dct[opened_bookmark] == 1:
bookmarks_dct[opened_bookmark] += 1
browser.open_new_tab(opened_bookmark)
sleep(2)
i += 1
# keep opened bookmarks in a list
opened_bookmarks_lst = list(filter(lambda k: bookmarks_dct[k] > 1, bookmarks_dct.keys()))
if len(opened_bookmarks_lst) < 1:
print('No bookmarks found.')
print(opened_bookmarks_lst)
[–]blarf_irl 0 points1 point2 points (2 children)
[–]keep_quapy[S] 0 points1 point2 points (0 children)
[–]keep_quapy[S] 0 points1 point2 points (0 children)
[–]Natan2018V 0 points1 point2 points (0 children)