我刚刚开始探索 斜纹.

Twill是一种令人惊叹的脚本浏览语言,它可以完成我想要的!!!

到目前为止,我一直在使用python shell(闲置(python gui) 确切地说),我以一个人执行命令的方式做事(我的意思是,我键入一个命令,运行它,然后键入下一个命令):alt text
(来源: narod.ru)

alt text
(来源: narod.ru)

alt text
(来源: narod.ru)

alt text
(来源: narod.ru)

alt text
(来源: narod.ru)

alt text
(来源: narod.ru)

alt text
(来源: narod.ru)

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

似乎在Twill中存在这种可能性。此示例从Twill文档页面(您可以看到它) 这里)向我们展示一件由几个命令组成的代码: alt text
(来源: narod.ru)

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


更新1:

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

您好,S.Mark !!!对不起,我们的回复很晚。首先,有关我的Twill和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命令(我一定是做错了事):alt text
(来源: narod.ru)

alt text
(来源: narod.ru)

但是您知道什么,我决定进行一些试验并创建一个test.py文件,该文件也只包含这两个命令。我还将此文件放置在Twill-0.9文件夹中(e: tmp twill-0.9 test.py),然后我决定尝试使用Twill的Runfile命令尝试从Twill Shell运行它,而且令人惊讶的是,它可以使用! :alt text
(来源: narod.ru)

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

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

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


更新2:

(2010年4月23日,星期五,上午3:48:15(GMT+0.00))

(此更新是我对S.Mark的第二个回复)

似乎从命令提示符运行它也不成功:alt text
(来源: narod.ru)

有帮助吗?

解决方案

例如,将您的斜纹命令放入文件中 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

您可能需要检查.twill示例代码 tests 斜纹源文件夹

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
....

其他提示

在这里,它正在采取行动(更改了一个):

>>> 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,所以我用以下来安装美丽的小组和斜纹:

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

这如何有所帮助

一种

我认为,您不应该使用Twill Python API直接调用功能,而不是使用Twill shell http://twill.idyll.org/python-api.html.

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)
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top