目錄
介紹
Homepage: http://wwwsearch.sourceforge.net/mechanize/
mechanize.Browser and mechanize.UserAgentBase implement the interface of urllib2.OpenerDirector
mechanize.UserAgentBase offers easy dynamic configuration of user-agent features like
protocol, cookie, redirection and robots.txt handling (by calling build_opener())
Form
# Let's search br.form['q']='weekend codes' br.submit() print br.response().read() for link in br.links(url_regex="python.org"): print link
Download
f = br.retrieve('http://www.google.com.br/intl/pt-BR_br/images/logo.gif')[0] print f fh = open(f)
Upload
# add a file, default value for MIME type, no filename sent to server form.add_file(open("data.dat")) # add a second file, explicitly giving MIME type, and telling the server # what the filename is form.add_file(open("data.txt"), "text/plain", "data.txt")
Link
找出 page 內的所有 link
for link in br.links(): print link
找出 link 有 'python.org' 這字串
for link in br.links(url_regex="python.org"): print link
Click 某一條 link
br.follow_link(link)
# Return mechanize.Link objects
find_link(text_regex=re.compile("python"), nr=2)
# exactly "monty python".
find_link(text="monty python")
GET / POST
import mechanize cookies = mechanize.CookieJar() # build_opener() adds standard handlers (such as HTTPHandler and # HTTPCookieProcessor) by default. The cookie processor we supply # will replace the default one. opener = mechanize.build_opener(mechanize.HTTPCookieProcessor(cookies)) r = opener.open("http://example.com/") # GET r = opener.open("http://example.com/", data) # POST