本文介绍了如何将所有斜纹命令放到.py文件中的一段代码中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始探索 TWILL .

I have just started exploring TWILL.

Twill是一种很棒的Web浏览脚本语言,它可以满足我的所有需求!!!

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

到目前为止,我一直在使用Python外壳(准确地说是 IDLE(Python GUI))中的斜纹布,并且我在那里以一种逐个执行命令的方式进行操作(我的意思是,我键入一个命令,运行它,然后键入下一个命令):

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):













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

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

所以,我的问题是:如何将所有命令以斜纹方式组合在一起?

So, my question is: How can I put all commands together in twill?

更新1:

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

(this update is my response to S.Mark)

您好,S.Mark !!!对不起,我很抱歉.首先,有关我的斜纹和python相关文件夹的位置的一些信息:

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:

在我的计算机上安装Python2.5的路径:C:\ Python25

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

我的计算机上的twill-0.9的路径现在为:E:\ tmp \ twill-0.9

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

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

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

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

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):



但是您知道吗,我决定尝试一下,并创建了一个只包含这两个命令的test.py文件.我还将该文件放置在twill-0.9文件夹中(E:\ tmp \ twill-0.9 \ test.py),然后我决定尝试使用twill的runfile命令从twill shell运行该文件,令人惊讶的是,它起作用了! :

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! :

运行它之后,我在C:\ Python25文件夹中查找并在其中找到了新创建的result.html文件!

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

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

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.

我的下一步是尝试在"Google App Engine"上运行类似的代码,但是据我所知,只有Python被识别,而不是twill,这意味着如果我只知道如何在其中执行操作斜纹,但不是在python中,我将无法让"Google App Engine"执行命令.

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.

更新2:

(2010年4月23日,星期五,上午3:48:15(格林尼治标准时间+0.00))

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

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

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

在命令提示符下运行它似乎也不成功:

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

推荐答案

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

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

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

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

python twill-sh test.twill

您可能要检查twill源的tests文件夹中的.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
....

这篇关于如何将所有斜纹命令放到.py文件中的一段代码中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 14:39