How to log in to a website using installed twill?
Asked Answered
C

4

6

I have just successfully installed TWILL on my computer with the help of one very supportive member of "StackOverflow" (you can check it out HERE) and have tried to run one of the simple examples on the twill documentation page (you can see that page HERE). Here is that example:

alt text
(source: narod.ru)

Let's say my username on www.slash.org is lynxye and my password is mammal. When I try to enter that example code into my Python prompt, I can only type and enter the first line of the code because when I click on "Enter" to start a new line, I get some error messages right away:

alt text
(source: narod.ru)

The same happens when I try to enter this code into my terminal:

alt text
(source: narod.ru)

I think I miss out on some basics here. Perhaps, I need to create a file that would contain that code and then run that file somehow, but I really don't know where I need to create that file and with what extensdion.

Can anyone, please, help me with this?

Charla answered 21/4, 2010 at 3:51 Comment(0)
L
4

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*
>>
Lutherlutheran answered 21/4, 2010 at 4:2 Comment(3)
Hello, S.Mark!!! Do you know where that twill shell could be located? There is one file called "twill-sh" in my "twill-0.9" folder. Is that the twill shell? Strange, it's blank - I mean, it has no extension.Charla
Hi, yeah, run like python twill-shLutherlutheran
or just rename it to twill-sh.py :-) then you can do twill-sh.py in command prompt.Lutherlutheran
I
3

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

Ivatts answered 20/5, 2010 at 20:2 Comment(3)
Thakn You, amadain!!! Can You, please, tell me, what does the second line of Your code (>>> import re) do?Charla
import re - import the regular expression module. Are you not used to python? I would suggest running through the tutorial on www.python.org. It is a very good tutorial to get you started on pythonIvatts
Thank You, amadain!!! Yes, I am just a beginner in python. Thank You for referring me to that tutorial - a lot of my questions are explained there!Charla
F
0

The angle brackets aren't meant to be entered literally; they're just there to indicate you should enter a value. Try

setlocal username lynxye

instead.

Frink answered 21/4, 2010 at 3:53 Comment(0)
C
0

That's not the way to use in prompt.

You need to enclose key-value pair in brackets. Like,

setlocal('uname','xyz')

setlocal('password','123')

go('http://abc.org')

fv('1','foo','bar')

Cytogenesis answered 1/8, 2013 at 8:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.