Вы находитесь на странице: 1из 2

#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#Takes a list of words from a specified file, runs them # through dictionary.

com and prints the definitions in a # new text document. Very useful for when your teacher # wants to give you homework, but there's nothing left # to assign, so she makes you copy 127 definitions from # the back of the book due the next day making you realize # what a waste of time the class is so you just make this # program instead... Anyway, here it is. # #Note: Only takes the first definition from each search. You can easily # change this by making it look for 2.</td><td> rather than 1. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=import urllib2 #imports urllib2... can't get much simpler than that print '-------------------------------------------------------------------------' print 'Hello and welcome to my dictionary program. In order to use it, you must ' print 'create a text file in the same directory as the program. In the text fil e,' print 'you have to make a list of words you need to be defined, but' print 'make sure they\'re each on a new line or it won\'t work. The script then runs' print 'through the list and places each word into a search on dictionary.com.' print 'It then copies the definitions and saves them into the specified file.' print ' Enjoy' print print 'Created By: Futility' print 'Written In: Python' print '-------------------------------------------------------------------------' print saveTo = raw_input("What would you like the output file to be named? (Be sure to include the .txt)\n") DefFile = open(saveTo,"w") #Opens the file where the definitions will be saved wordlist = raw_input("What text file will the words be located in? (Once again, .txt is important)\n") f = open(wordlist) #Opens the file where the words are for line in f: #starts the loop that'll read through your list of words page = urllib2.urlopen('http://dictionary.reference.com/browse/' + line) #op ens the dictionary.com page for your words html = page.read() #saves the source of the opened page #Get the definition pos = html.find('1.</td> <td>')#Find where you need to start from the source definition = html[pos+12:]#Cut out all the unnecessary tags endpos = definition.find('<')#Define the end position as the start of a new tag definition = definition[:endpos]#Combine the start and stop to get the full definition DefFile.write(line + definition + '\n\n')#Write the definition to the file w ith two newlines for formatting purposes print 'Congratulations, you\'re done. Check ' + saveTo + ' for all your new def initions.'

print '

You\'re Welcome.'

#Close the files. Not really necessary, but I do it anyway. f.close() DefFile.close()

Вам также может понравиться