Skip to main content
gamedev.net
🔒 Locked

Python String Issues

Started by CodeTitan 4 replies 600+ views
Original Post
CodeTitan
CodeTitan

from urllib2 import *

print "Enter a word, and this program will find its definition."
word = raw_input()
dict_url = "http://dictionary.reference.com/search?q=" + word

for line in urlopen(dict_url):
	if '< ! - - begin ahd4 - - > ' in line: #look for comment
        #this is the part where I get confused



I want to write a program that allows me to get the definitions of words off of Dictionary.com. I found out that the website conveniently inserts comment tags to mark off the definition, so I decided to test out my new Python skills and tried to write a program that would retrieve the definition for me. Unfortunately, I'm having some troubles. After I check if the comment beginning delimiter (< ! - - begin ahd4 - - >) is in the line, I want to keep printing lines until the ending delimiter is met (< ! - - end ahd4 - - >). Unfortunately, this usually ends in an infinite loop, or something of the sort. I'm not sure how to get it to print only what is between the two comment lines. I've tried putting a loop that says something like while not '< ! - - end ahd4 - - >' in line:, but that doesn't work. Can someone please help me? EDIT: I had to put spaces in the comment tags, otherwise they wouldn't show up.
smr
smr
from urllib2 import *print "Enter a word, and this program will find its definition."word = raw_input()dict_url = "http://dictionary.reference.com/search?q=" + wordprintlines = Falsefor line in urlopen(dict_url):    # replace the == with --    if '<!== begin ahd4 -->' in line:        printlines = True    elif '<!== end ahd4 -->' in line:        printlines = False    if printlines:        print line
CodeTitan
CodeTitan
Yahoo, it works! I did something like that originally, but instead of just setting the flag in the if statements, I actually used the print function. So that problem is solved. Now all I need to do is figure out Regular Expressions in order to get rid of all those HTML tags...

Trap
Trap
Best program for learning regular expressions:
regex-coach
smr
smr
Good.

You've got your work cut out for you as far as extracting the definitions. There's a lot of HTML there for your regular expressions to get confused over. My suggestion is to not use dictionary.com. I would suggest using a dictionary web service... now, finding a free dictionary web service is the real challenge.
CodeTitan
CodeTitan
Quote:
Original post by smr
Good.

You've got your work cut out for you as far as extracting the definitions. There's a lot of HTML there for your regular expressions to get confused over. My suggestion is to not use dictionary.com. I would suggest using a dictionary web service... now, finding a free dictionary web service is the real challenge.


I have an idea to remove only certain tags, and output to a HTML file. This way, I won't have to cut out all of the tags, thus saving me time.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.