1

我刚刚开始探索TWILL

Twill 是一种用于 Web 浏览的令人惊叹的脚本语言,它可以满足我的所有需求!!!

到目前为止,我一直在使用 Python shell 中的斜纹布(准确地说是IDLE (Python GUI)),并且我在那里做事的方式是一个接一个地执行命令(我的意思是,我输入一个命令,运行它,然后输入下一条命令):(来源:narod.ru替代文字

替代文字
(来源:narod.ru

替代文字
(来源:narod.ru

替代文字
(来源:narod.ru

替代文字
(来源:narod.ru

替代文字
(来源:narod.ru

替代文字
(来源:narod.ru

但是我不知道如何将所有这些命令放在一个 .py 文件中,以便它们都将自动执行。

斜纹布似乎有这样的可能性。这个来自斜纹文档页面的例子(你可以在这里看到)向我们展示了一段由几个命令组成的代码:(来源:narod.ru替代文字

所以,我的问题是:如何将所有命令放在斜纹中?


更新1:

(此更新是我对 S.Mark 的回应)

你好,S.马克!!!对于迟到的回复,我深表歉意。首先,关于我的斜纹和 python 相关文件夹的位置的一些信息:

我的电脑上安装Python2.5的路径:C:\Python25

现在我电脑上的twill-0.9的路径:E:\tmp\twill-0.9

假设我希望自动执行以下命令:

go http://www.yahoo.com

save_html result.html

此代码应查看 yahoo 页面,然后将其 HTML 代码保存到 result.html 文件中。因此,尝试按照您的指示,我首先创建了包含此代码的“test.txt”文件,该文件仅包含 2 行,并将该文件保存为 twill-0.9 文件夹中的“test.twill”,这意味着该文件的完整路径文件现在是 E:\tmp\twill-0.9\test.twill

然后我尝试以许多不同的方式将文件名作为参数传递给 twill-sh 命令,但它从未起作用(我一定是做错了什么):( 来源:narod.ru替代文字

替代文字
(来源:narod.ru

但是你知道吗,我决定尝试一下,并创建了一个 test.py 文件,其中也只包含这两个命令。这个文件我也放在 twill-0.9 文件夹 (E:\tmp\twill-0.9\test.py) 中,然后我决定尝试使用 twill 的 runfile 命令从 twill shell 运行它,令人惊讶的是,它成功了!:( 来源:narod.ru替代文字

运行后,我查看了我的 C:\Python25 文件夹,并在那里找到了新创建的 result.html 文件!

好吧,我在这里所做的只是使用 twill 命令从 twill shell 运行一个文件。虽然目前这正是我所需要的,但其他支持者(如下所示)建议我应该从 python shell 做所有事情,而不是从 twill shell,这是我仍然不知道该怎么做的事情。

我下一步将尝试在“Google App Engine”上运行类似的代码,但据我所知,那里只识别 Python,而不识别斜纹,这意味着如果我只知道如何在斜纹中做事,但是不在 python 中,我将无法让“Google App Engine”执行我的命令。


更新 2:

(2010 年 4 月 23 日星期五凌晨 3:48:15 (GMT+0.00))

(此更新是我对 S.Mark 的第二次回应)

似乎从命令提示符运行它也不成功:( 来源:narod.ru替代文字

4

4 回答 4

3

这是在行动(改变了一点):

>>> 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/twill@lists.idyll.org/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/twill@lists.idyll.org/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>
>>>  

我使用 ubuntu,所以我使用以下安装 BeautifulSoup 和 twill:

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

这有什么帮助

一个

于 2010-05-20T15:54:45.273 回答
3

例如,将斜纹命令放入文件中test.twill

setlocal query "twill Python"

go http://google.com/

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

show

然后只需将文件名作为参数传递给 twill-sh 命令,例如

python twill-sh test.twill

您可能想检查tests斜纹源文件夹中的.twill 示例代码

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
....
于 2010-04-22T05:44:57.997 回答
2

我认为不要使用斜纹外壳,而应该直接使用斜纹 python api 调用函数http://twill.idyll.org/python-api.html

于 2010-04-22T05:41:40.863 回答
1
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)
于 2010-04-22T14:12:06.007 回答