You are trying to select a form named q
, which does not exist. It seems that the form is named f
instead. (However, I was unable to verify that in my browser - even with Javascript disabled, I only saw a different name.)
A simple Google search can be done like this:
import os, subprocess
import re
import mechanize
from bs4 import BeautifulSoup
#prepare mechanize
br = mechanize.Browser()
br.set_handle_robots(False)
br.set_handle_equiv(False)
br.addheaders = [('User-agent', 'Mozilla/5.0')]
br.open('http://www.google.com/')
# do the query
br.select_form(name='f') # Note: select the form named 'f' here
br.form['q'] = 'here goes your query' # query
data = br.submit()
# parse and output
soup = BeautifulSoup(data.read())
print soup
This should give you the idea.
Update: How to find the right form 'selector'
To print the names of the available forms, you can do:
for form in br.forms():
print form.name
This comes in handy when you use the interactive console.
You are not bound to use the name of the form, but you may give other hints to select the right form. For example, on some pages the forms have no name at all. Then you can still select based on the number of the form, e.g. br.select_form(nr=1)
for the second form on the page. Please see help(br.select_form)
for details. Also, list(br.forms())
will give you a list of all forms which you can inspect further.
Another option would be to inspect the page by hand in your usual browser.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…