views:

220

answers:

4

Hello everybody!

I have just started exploring TWILL.

Twill is an amazing scripting language for Web browsing and it does all I want!!!

So far I've been using twill from a Python shell (IDLE (Python GUI) to be precise) and I do things there in the way of executing commands one by one (I mean, I type one command, run it, then type the next command):alt text alt text alt text alt text alt text alt text alt text But I don't know how to put all these commands together in one .py file, so that they would all be executed one by one automatically.

It seems that there is such possibility in twill. This example from the twill documentation page (you can see it HERE) shows us one piece of code consisting of several commands: alt text So, my question is: How can I put all commands together in twill?


Update 1:

(this update is my response to S.Mark)

Hello, S.Mark!!! I am sorry for the late response. First of all, some info about the location of my twill and python related folders:

The path where Python2.5 is installed on my computer: C:\Python25

The path to my twill-0.9 on my computer now: E:\tmp\twill-0.9

Let’s say I want the following commands to be carried out automatically:

go http://www.yahoo.com

save_html result.html

This code should look into yahoo page and then save its HTML code into result.html file. So, trying to follow Your instructions, I firstly created “test.txt” file containing this code consisting of only 2 lines and saved that file as “test.twill” in the twill-0.9 folder, which means that the full path to that file now was E:\tmp\twill-0.9\test.twill

Then I tried to pass the file name as parameter to twill-sh command in many different ways, but it never worked (I must’ve been doing something wrong): alt text alt text But you know what, I decided to experiment a bit and created a test.py file that also contained only those two commands. This file I also placed in the twill-0.9 folder (E:\tmp\twill-0.9\test.py) and then I decided to try running it from twill shell using twill’s runfile command, and, surprisingly, it worked! : alt text

After running it, I looked up my C:\Python25 folder and found the newly-created result.html file there!

Well, what I've done here is simply running a file from the twill shell using a twill command. While at the moment it is exactly what I need, other supporters (as you can see below) suggest I should do all things from python shell, not from twill shell, and that is something that I still don’t know how to do.

My next step will be to try running a similar code on “Google App Engine”, but there, as far as I know, only Python is recognized, not twill, which means that if I only know how to do things in twill, but not in python, I won’t be able to have “Google App Engine” execute my commands.


Update 2:

(Friday 23, April, 2010, 3:48:15 a.m.(GMT+0.00))

(This update is my second response to S.Mark)

It seems that running it from command prompt isn't successful either: alt text

+2  A: 

I think that instead of using the twill shell, you should instead directly call the functions using the twill python api http://twill.idyll.org/python-api.html.

Nikwin
Thank You, Nikwin. I think You are right - I really need to learn ho to do it from python shell.
brilliant
+3  A: 

Put your twill commands into a file, for example test.twill

setlocal query "twill Python"

go http://google.com/

fv 1 q $query
submit btnI     # use the "I'm feeling lucky" button

show

And then just pass filename as parameter to twill-sh command, like

python twill-sh test.twill

And you might want to check .twill sample codes in tests folder of twill source

test-back.twill
test-basic.twill
test-dns.twill
test-equiv-refresh.twill
test-find.twill
test-form.twill
test-formfill.twill
test-global-form.twill
test-go-exit.twill
....
S.Mark
Hello, S.Mark!!! I've tried Your way and something didn't work out there, but again trial-and-error method was a help. Please refer to "Update 1" section above. Thank You for Your time.
brilliant
@ S.Mark: "for update 1, you need to run python twill-sh test.twill from command prompt" - Am I missing something important there? It just doesn't want to go through command prompt. Please, take a look at the screen shot in "Update 2". Thank You.
brilliant
@ S.Mark: "regarding update 2, looks like adding C:\Python25; to PATH environment variable didn't succeed"- Ouch! I am soooo sorry! It's my fault - I switched to another computer and didn't tell You about it. I thought that informing You about the change of the path of my "twill-0.9" folder was just enough; and I completely forgot about this need to edit the "Path" environment variable! Yes, I just tried both Your quick method (twill-sh.py) and the longer one (python twill-sh , with "Path" edited) - they both work!!! It's interesting that if I do it one of
brilliant
these 2 ways, the "result.html" file appears in the “twill-0.9” folder, but when I use the twill’s runfile command, “result.html” is created in “Python25”. Anyway, now it can be done from the command prompt!!! Thank You, and I am sorry again.
brilliant
@brilliant, regarding "result.html", try `save_html E:\tmp\twill-0.9\result.html` instead of `save_html result.html`
S.Mark
@ S.Mark: Yes, that works! Now even twill's-runfile-command way creates the "result.html" in "twill-0.9" !!! Thank You!!!
brilliant
Welcome brilliant
S.Mark
Hello, S.Mark!!! I am sorry to bother You again, but it seems that I got stuck with twill - having problems trying to run it from Python shell (not from twill shell). I am even thinking now to switch to mechanize, although I've spent so much time and energy on learning twill. If You have time and willingness, please, check this page, where I asked a new question about it: http://stackoverflow.com/questions/3621432/using-twill-from-python-to-open-a-link-module-object-has-no-attribute-popen
brilliant
+1  A: 
import string, re, sys, os
import twill.commands

class browser:
   def __init__(self, url="www.google.com", query="python code", log = None):
      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.log = log
      self.b.clear_cookies()
      self.url=url
      self.b.go(self.url)
      f = self.b.get_form("1")
#      self.log.debug("form is %s" % f)
      f["q"] = query
      self.b.submit()
      self.log.debug( "Links\n%s" % self.b.showlinks())
      self.log.debug( "Forms\n%s" % self.b.showforms())
      pageContent = self.b.get_html()
      self.log.debug("html is <<%s>>" % pageContent)
amadain
Thank You, user323303, for providing this code. But I hardly understand a thing in it. I will take some time to study it.
brilliant
+2  A: 

Here it is in action (changed a wee bit):

>>> import twill.commands
>>> import BeautifulSoup
>>> 
>>> class browser:
...    def __init__(self, url="http://www.google.com",log = None):
...       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.log = log
...       self.b.clear_cookies()
...       self.url=url
...    def googleQuery(self, query="python code"):
...       self.b.go(self.url)
...       #self.b.showforms()
...       f = self.b.get_form("f")
...       #print "form is %s" % f
...       f["q"] = query
...       self.b.clicked(f, "btnG")
...       self.b.submit()
...       pageContent = self.b.get_html()
...       soup=BeautifulSoup.BeautifulSoup(pageContent)
...       ths = soup.findAll(attrs={"class" : "l"})
...       for a in ths:
...          print a
... 
>>> t=browser()
>>> t.googleQuery("twill queries")
==> at http://www.google.ie/
Note: submit is using submit button: name="btnG", value="Google Search"

<a href="http://pyparsing.wikispaces.com/WhosUsingPyparsing" class="l" onmousedown="return clk(this.href,'','','res','1','','0CBMQFjAA')">pyparsing - WhosUsingPyparsing</a>
<a href="http://www.mail-archive.com/[email protected]/msg00048.html" class="l" onmousedown="return clk(this.href,'','','res','2','','0CBcQFjAB')">Re: [<em>twill</em>] <em>query</em>: docs, and web site.</a>
<a href="http://www.mail-archive.com/[email protected]/msg00050.html" class="l" onmousedown="return clk(this.href,'','','res','3','','0CBkQFjAC')">Re: [<em>twill</em>] <em>query</em>: docs, and web site.</a>
<a href="http://www.genealogytoday.com/surname/finder.mv?Surname=Twill" class="l" onmousedown="return clk(this.href,'','','res','4','','0CB4QFjAD')"><em>Twill</em> Genealogy and Family Tree Resources - Surname Finder</a>
<a href="http://a706cheap-apparel.hobby-site.com/ladies-cotton-faded-twill-le-chameau-breeks-42" class="l" onmousedown="return clk(this.href,'','','res','5','','0CCEQFjAE')">Ladies Cotton Faded <em>Twill</em> Le Chameau Breeks 42</a>
<a href="http://twill.idyll.org/examples.html" class="l" onmousedown="return clk(this.href,'','','res','6','','0CCMQFjAF')"><em>twill</em> Examples</a>
<a href="http://panjiva.com/Sri-Lankan-Manufacturers-Of/twill+capri" class="l" onmousedown="return clk(this.href,'','','res','7','','0CCcQFjAG')">Sri-Lankan <em>Twill</em> Capri Manufacturers | Sri-Lankan Suppliers of <b>...</b></a>
<a href="http://c586cheap-apparel.dyndns.ws/twill-beige-blazer" class="l" onmousedown="return clk(this.href,'','','res','8','','0CCoQFjAH')"><em>Twill</em> beige blazer</a>
<a href="http://stackoverflow.com/questions/2267537/how-do-you-use-relative-paths-for-twill-tests" class="l" onmousedown="return clk(this.href,'','','res','9','','0CCwQFjAI')">How do you use Relative Paths for <em>Twill</em> tests? - Stack Overflow</a>
<a href="http://mytextilenotes.blogspot.com/2010/01/introduction-to-twill-weave.html" class="l" onmousedown="return clk(this.href,'','','res','10','','0CC8QFjAJ')">My Textile Notes: Introduction to <em>Twill</em> Weave</a>
>>>  

I use ubuntu so I use the following to install BeautifulSoup and twill:

sudo apt-get install BeautifulSoup*  
sudo apt-get install python-twill*

How this helps

A

amadain
Thank You, amadain, for this input. (Are You former user323303?). So, does it mean I have to download and install "Beautiful Soap" first?
brilliant
well beautifulsoup goes hand in hand with selenium and twill. Its very handy for parsing the pages once you get to the page you are looking for. You don't have to download it but I would recommend using it if you are planning to use twill or selenium a lot
amadain
and yes I am former user323303
amadain
I see. Thank You, amadain!!! (Sorry for the late response).
brilliant