1

ツイルの探索を始めたばかりです。

ツイルはWebブラウジングのための素晴らしいスクリプト言語であり、私が望むすべてを実行します!!!

これまで、Pythonシェル(正確にはIDLE(Python GUI))のツイルを使用しており、コマンドを1つずつ実行する方法で実行しています(つまり、1つのコマンドを入力して実行し、次に次のように入力します)。次のコマンド):(ソース:narod.ru代替テキスト

代替テキスト
(出典:narod.ru

代替テキスト
(出典:narod.ru

代替テキスト
(出典:narod.ru

代替テキスト
(出典:narod.ru

代替テキスト
(出典:narod.ru

代替テキスト
(出典:narod.ru

しかし、これらすべてのコマンドを1つの.pyファイルにまとめて、すべてが1つずつ自動的に実行されるようにする方法がわかりません。

ツイルにはそういう可能性があるようです。ツイルのドキュメントページ(ここで確認できます)のこの例は、いくつかのコマンドで構成される1つのコードを示しています:(ソースnarod.ru代替テキスト

だから、私の質問は:どうすればすべてのコマンドをツイルにまとめることができますか?


アップデート1:

(この更新はS.Markへの私の応答です)

こんにちは、S.Mark !!! 返信が遅くなってすみません。まず第一に、私のツイルとPython関連のフォルダーの場所に関するいくつかの情報:

Python2.5が私のコンピューターにインストールされているパス:C:\ Python25

私のコンピューター上のツイル-0.9へのパス:E:\ tmp \ twill-0.9

次のコマンドを自動的に実行したいとします。

go http://www.yahoo.com

save_html result.html

このコードはyahooページを調べてから、そのHTMLコードをresult.htmlファイルに保存する必要があります。それで、あなたの指示に従おうとして、私は最初に2行だけで構成されるこのコードを含む「test.txt」ファイルを作成し、そのファイルを「test.twill」としてtwill-0.9フォルダーに保存しました。ファイルはE:\ tmp \ twill-0.9\test.twillになりました

次に、ファイル名をパラメーターとしてtwill-shコマンドにさまざまな方法で渡そうとしましたが、機能しませんでした(何か間違ったことをしているに違いありません):( ソース:narod.ru代替テキスト

代替テキスト
(出典:narod.ru

しかし、ご存知のとおり、私は少し実験して、これら2つのコマンドのみを含むtest.pyファイルを作成することにしました。このファイルもtwill-0.9フォルダー(E:\ tmp \ twill-0.9 \ test.py)に配置し、twillのrunfileコマンドを使用してtwillシェルから実行してみることにしました。驚くべきことに、このファイルは機能しました。:( ソース:narod.ru代替テキスト

それを実行した後、C:\ Python25フォルダーを調べて、そこに新しく作成されたresult.htmlファイルを見つけました!

ここで行ったことは、twillコマンドを使用してtwillシェルからファイルを実行することです。現時点ではまさにそれが必要ですが、他のサポーター(以下を参照)は、ツイルシェルからではなく、Pythonシェルからすべてのことを実行する必要があることを示唆しています。これは、まだ方法がわかりません。

次のステップは、「Google App Engine」で同様のコードを実行することですが、私が知る限り、Pythonのみが認識され、ツイルは認識されません。つまり、ツイルでの操作方法しかわからない場合は、 Pythonではないため、「GoogleAppEngine」でコマンドを実行することはできません。


アップデート2:

(2010年4月23日金曜日、午前3時48分15秒(GMT + 0.00))

(この更新は、S.Markに対する私の2番目の応答です)

コマンドプロンプトからの実行も成功していないようです:( 出典:narod.ru代替テキスト

4

4 に答える 4

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

次に、filenameをパラメータとして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 に答える
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とツイルをインストールするために以下を使用します。

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

これがどのように役立つか

A

于 2010-05-20T15:54:45.273 に答える
2

twillシェルを使用する代わりに、twillpythonapiを使用して関数を直接呼び出す必要があると思います。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 に答える