How to log in to a website using installed twill?
You need to run those commands inside twill shell, instead of python shell
D:\tmp\twill-0.9>python twill-sh
-= Welcome to twill! =-
current page: *empty page*
>>
You can put twill commands when you see >>
or there is 2 only lines in twill-sh file
import twill.shell
twill.shell.main()
you can just copy paste that 2 lines to python prompt.
>>> import twill.shell
>>> twill.shell.main()
-= Welcome to twill! =-
current page: *empty page*
>> ?
Undocumented commands:
======================
add_auth fa info save_html title
add_extra_header find load_cookies setglobal url
agent follow notfind setlocal
back formaction redirect_error show
clear_cookies formclear redirect_output show_cookies
clear_extra_headers formfile reload show_extra_headers
code formvalue reset_browser showforms
config fv reset_error showhistory
debug get_browser reset_output showlinks
echo getinput run sleep
exit getpassword runfile submit
extend_with go save_cookies tidy_ok
current page: *empty page*
>>
This'll do it:
>>> import twill.commands
>>> import re
>>>
>>> class browser:
... def __init__(self, url="http://www.slashdot.org"):
... self.a=twill.commands
... self.a.config("readonly_controls_writeable", 1)
... self.b = self.a.get_browser()
... self.b.set_agent_string("Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14")
... self.b.clear_cookies()
... self.url=url
... def slashdot(self, loginName, passwd):
... self.b.go(self.url)
... f = self.b.get_form("1")
... regexp = re.compile("Log In")
... link = self.b.find_link(regexp)
... if link:
... self.b.follow_link(link)
... f=self.b.get_form("2")
... f["unickname"] = loginName
... f["upasswd"] = passwd
... self.a.fv("2", "userlogin", "")
...
>>>
>>> t=browser()
>>> t.slashdot("username", "password")
==> at http://slashdot.org/
==> at http://slashdot.org/my/login
forcing read-only form field to writeable
>>>
Its also easier if you use BeautifulSoup to parse the html