/
browser.py
72 lines (52 loc) · 1.96 KB
/
browser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python
import twill.commands
from twill.utils import print_form
import twill
class Browser:
def __init__ (self, ua="robotina-ua", timeout=200):
self.commands = twill.commands
self.commands.add_extra_header("User-Agent", ua)
self.commands.timeout = timeout
def source_code (self, url=None):
if url:
try:
self.commands.go(url)
except:
print "There is a problem with the URL"
sc = self.commands.get_browser().get_html()
else:
sc = "Please provide the URL"
return sc
def show_forms (self, url, filename="forms"):
if url:
try:
self.commands.go(url)
except:
print "There is a problem with the URL"
forms = self.commands.get_browser().get_all_forms()
stream = open(filename, 'w')
for i, form in enumerate(forms):
print_form (i,form,stream)
else:
sc = "Please provide the URL"
def simple_login (self, url = None, form_number = None, field_name = None,
field_password = None, value_name =None, value_password =None):
if url:
try:
self.commands.go(url)
except:
print "There is a problem with the URL"
self.commands.formclear(form_number)
self.commands.fv(form_number, field_name, value_name)
self.commands.fv(form_number, field_password, value_password)
self.commands.submit()
else:
response = "Please provide the URL"
return self.commands.get_browser()
#return self.commands.get_browser().get_url()
#return self.commands.get_browser().get_html()
#return self.commands.get_browser().get_title()
def main():
print "."
if __name__ == "__main__":
exit (main())