Skip to content

Instantly share code, notes, and snippets.

@timothycarambat
Created May 14, 2018 01:03
Show Gist options
  • Save timothycarambat/b71e30d994dd5401bcdae6bc88983d83 to your computer and use it in GitHub Desktop.
Save timothycarambat/b71e30d994dd5401bcdae6bc88983d83 to your computer and use it in GitHub Desktop.
Fetch word Pronunciation from Dictionary.com
# This file will write to console text that contains the pronunciation of words in
# an array. It doesnt output as a file, but with about three lines it could.
import urllib2
from bs4 import BeautifulSoup
#establish source URL
base_url = "http://www.dictionary.com/browse/%s?s=t"
#Strip brackts from pronunciation returned
strip_brackets = False
#establish words to find
word_list =[
'name',
'sample',
'diction',
'cerulean',
'defenestrate',
'homepage',
'notaword',
'rose'
]
for word in word_list:
try:
page = urllib2.urlopen(base_url % word)
soup = BeautifulSoup(page, 'html.parser')
pron = soup.find('span', attrs={'class': 'spellpron'}).text
except:
print 'The word %s could not be processed.' % word
continue
if strip_brackets:
pron = pron.replace('[','').replace(']','')
print "The word %s is pronounced %s" % (word,pron)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment