mechanize python le

Solutions on MaxInterview for mechanize python le by the best coders in the world

showing results for - "mechanize python le"
Nicolás
13 Oct 2018
1import re
2import mechanize
3
4br = mechanize.Browser()
5br.open("http://www.example.com/")
6# follow second link with element text matching regular expression
7response1 = br.follow_link(text_regex=r"cheese\s*shop", nr=1)
8print(br.title())
9print(response1.geturl())
10print(response1.info())  # headers
11print(response1.read())  # body
12
13br.select_form(name="order")
14# Browser passes through unknown attributes (including methods)
15# to the selected HTMLForm.
16br["cheeses"] = ["mozzarella", "caerphilly"]  # (the method here is __setitem__)
17# Submit current form.  Browser calls .close() on the current response on
18# navigation, so this closes response1
19response2 = br.submit()
20
21# print currently selected form (don't call .submit() on this, use br.submit())
22print(br.form)
23
24response3 = br.back()  # back to cheese shop (same data as response1)
25# the history mechanism returns cached response objects
26# we can still use the response, even though it was .close()d
27response3.get_data()  # like .seek(0) followed by .read()
28response4 = br.reload()  # fetches from server
29
30for form in br.forms():
31    print(form)
32# .links() optionally accepts the keyword args of .follow_/.find_link()
33for link in br.links(url_regex="python.org"):
34    print(link)
35    br.follow_link(link)  # takes EITHER Link instance OR keyword args
36    br.back()
37