Get randow words from Internet with Python

In case you could need to get random words for a game or anything else, you could use this code:

''' get 4 random words '''
from urllib.request import Request, urlopen
import random
 

url="https://svnweb.freebsd.org/csrg/share/dict/words?revision=61569&view=co"
# request object pointing at the url
req = Request(url, headers={'User-Agent': 'Mozilla/5.0'})

# all the words in bytes
web_byte = urlopen(req).read()
# decoding into a string with all words
webpage = web_byte.decode('utf-8')
# create a list
wdslst = webpage.splitlines()
fourwords = random.sample(wdslst, 4)
print(fourwords)

Subscribe to the newsletter for updates
Tkinter templates
Avatar My youtube channel

Twitter: @pythonprogrammi - python_pygame

Videos

Speech recognition game

Pygame's Platform Game

Other Pygame's posts

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.