すべてのTwillコマンドを.pyファイルの1つのコードにまとめるにはどうすればよいですか?
質問
探索を始めたばかりです ツイル.
Twillは、Webブラウジングのための素晴らしいスクリプト言語であり、私が望むすべてをします!!!
これまでのところ、私はPythonシェルからTwillを使用しています(アイドル(python gui) 正確に言うと)そして、私はコマンドを1つずつ実行する方法でそこに物事を行います(つまり、1つのコマンドを入力し、実行してから次のコマンドを入力します):
(ソース: narod.ru)
(ソース: narod.ru)
(ソース: narod.ru)
(ソース: narod.ru)
(ソース: narod.ru)
(ソース: narod.ru)
(ソース: narod.ru)
しかし、これらすべてのコマンドを1つのファイルにまとめる方法がわかりません。
Twillにはそのような可能性があるようです。 Twill Documentationページのこの例(あなたはそれを見ることができます ここ)いくつかのコマンドで構成されるコードの1つを表示します。
(ソース: narod.ru)
それで、私の質問は次のとおりです。どうすればすべてのコマンドをTwillにまとめることができますか?
更新1:
(この更新はS.Markへの私の応答です)
こんにちは、S.mark !!!応答が遅くなってすみません。まず第一に、私のツイルとPython関連のフォルダーの場所に関するいくつかの情報:
Python2.5が私のコンピューターにインストールされるパス:C: Python25
私のコンピューター上の私のツイル0.9へのパス今すぐ: tmp twill-0.9
次のコマンドを自動的に実行したいとしましょう。
go http://www.yahoo.com
save_html result.html
このコードはYahooページを調べてから、HTMLコードをresult.htmlファイルに保存する必要があります。そのため、指示に従って、最初に2行のみで構成されるこのコードを含む「test.txt」ファイルを作成し、そのファイルをTwill-0.9フォルダーに「test.twill」として保存しました。ファイルは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 Shellから実行してみることにしました。 :
(ソース: narod.ru)
実行した後、C: Python25フォルダーを調べて、新しく作成されたresult.htmlファイルが見つかりました。
さて、私がここでやったことは、Twillコマンドを使用してTwill Shellからファイルを実行するだけです。現時点では、まさに私が必要としているものですが、他のサポーター(以下を見ることができるように)は、Twill Shellからではなく、Python Shellからすべてのことをする必要があることを示唆しています。
私の次のステップは、「Google App Engine」で同様のコードを実行してみることですが、私が知る限り、Pythonのみが認識され、Twillではなく、Twillで物事を行う方法を知っているだけでなく、 Pythonではなく、「Google App Engine」にコマンドを実行することはできません。
更新2:
(2010年4月23日金曜日、午前3時48分15分(GMT+0.00))
(この更新は、S.Markに対する私の2番目の応答です)
コマンドプロンプトからそれを実行することも成功していないようです:
(ソース: 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
そして、filenameをパラメーターとしてtwill-shコマンドに渡すだけです。
python twill-sh test.twill
そして、あなたは.twillサンプルコードをチェックしたいかもしれません tests
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
....
他のヒント
ここで動作しています(少し変更しました):
>>> 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*
これがどのように役立つか
a
Twill Shellを使用する代わりに、Twill Python APIを使用して関数を直接呼び出す必要があると思います。 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)