0

I'm trying to make a dictionary program using dictionary.com for the source but I can't find the name of the search box in the source code

from mechanize import Browser

inp = raw_input("Enter Word: ")
Word = (inp)

SEARCH_PAGE = "http://dictionary.reference.com/"

browser = Browser()
browser.open( SEARCH_PAGE )
browser.select_form( nr=0 ) 

browser['name of search form'] = Word
browser.submit()

Can anyone help me make this work or help me find the name of the search bar in the HTML source?

1 Answer 1

1

You can look through the forms with the method Browser.forms(). Each of these forms has a variable called controls, a list of controls in the form. Each control in this list has a "name" variable. You can use these names to index with the browser, as you already know.

from mechanize import Browser

inp = raw_input("Enter Word: ")
Word = (inp)

SEARCH_PAGE = "http://dictionary.reference.com/"

browser = Browser()
browser.open( SEARCH_PAGE )

form = list(browser.forms())[0]                        #The first form
print form
names = map(lambda n: n.name, form.controls)
print names

browser.select_form( nr=0 ) 

browser[names[0]] = Word

txt = browser.submit().read()

#txt has the html from dictionary.reference.com
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.