2010-04-26 13 views
0

我在我的電腦上安裝了斜紋(之前安裝了Python 2.5),並且最近一直在使用它。如何將斜紋和Python合併爲一個可以在「Google App Engine」上運行的代碼?

Python是安裝在C盤我的電腦上:C:\ Python25

而且斜紋文件夾(「斜紋0.9」)就坐落於此:E:\ tmp目錄\斜紋0.9

這裏是,我一直在使用斜紋代碼:

go 「some website’s sign-in page URL」 
formvalue 2 userid 「my login」 
formvalue 2 pass 「my password」 
submit 
go 「URL of some other page from that website」 
save_html result.txt 

此代碼可以幫助我登錄到一個網站,在其中我有一個賬戶,記錄該網站的其他頁面的HTML代碼(即我只能在登錄後訪問),並將其存儲在名爲「result.txt」的文件中(當然,befor e使用此代碼我首先需要用真實的登錄名替換「我的登錄名」,用我的真實密碼替換「我的密碼」,「一些網站的登錄頁面URL」和「來自該網站的其他頁面的URL」該網站的編號爲2,該網站上的表格編號用作該網站登錄頁面上的登錄表單)

此代碼存儲在「test.twill」文件中,它是位於我的「twill-0.9」文件夾中:E:\ tmp \ twill-0.9 \ test.twill 我從我的命令提示符下運行這個文件:python twill -sh test.twill

現在, Google App Engine SDK「,並且也一直在使用它。

舉例來說,我一直在使用這種代碼:

import hashlib 
m = hashlib.md5() 
m.update("Nobody inspects") 
m.update(" the spammish repetition ") 
print m.hexdigest() 

此代碼可以幫助我改變了那句「沒有人檢查了spammish重複」成MD5摘要。

現在,我該如何將這兩段代碼放到一起,可以在「Google App Engine」上運行的python腳本中?

比方說,我希望我的代碼從「Google App Engine」登錄到網站,轉到該網站上的另一個頁面,記錄其HTML代碼(這是我的斜紋代碼所做的),並轉換此HTML代碼進入其MD5摘要(這是我的第二個代碼所做的)。那麼,我怎樣才能將這兩個代碼組合成一個Python代碼呢?

我想,應該通過導入斜紋布來完成,但怎麼辦呢?是否可以使用Python代碼 - 由「Google App Engine」運行的代碼 - 從互聯網上某處導入斜紋?或者,也許斜紋已經安裝在「Google App Engine」上?


更新1:

(本次更新是我的迴應Wooble的答案)

這裏是所有的文件夾列表(在我的「斜紋0.9」文件夾)包含__init __。 py文件。 (這個名單上的一些文件夾所在內的其他文件夾,這是在這個名單中還提到):

E:\斜紋0.9 \建造\ LIB \斜紋\分機\ match_parse

E:\斜紋-0.9 \ build \ lib \ twill \ extensions

E:\ twill-0。9 \建立\ lib中\斜紋\ other_packages \ _mechanize_dist

E:\斜紋0.9 \構建\ lib中\斜紋\ other_packages

E:\斜紋0.9 \構建\ lib中\斜紋

ë :\斜紋0.9 \斜紋\分機\ match_parse

E:\斜紋0.9 \斜紋\擴展

E:\斜紋0.9 \斜紋\ other_packages \ _mechanize_dist

E:\ twill- 0.9 \斜紋\其他_packages

E:\斜紋0.9 \斜紋

回答

1

要在App Engine項目中使用第三方庫,您只需在部署時將它們與您的應用程序一起使用。將斜紋文件夾(包含__init__.py的文件夾)複製到應用程序的文件夾中並進行部署。看看斜紋谷歌代碼項目,看起來twill在包中包含它的依賴關係(pyparsing,機械化等),所以你可能不需要包含任何其他東西。

+0

Hello Wooble !!!!非常感謝您的寶貴迴應!我剛剛查看了我的「twill-0.9」文件夾(我從下載的twill .zip包中提取的文件夾),並發現其中包含一個'__init__.py文件的文件夾。所以,現在我有點困惑,因爲我不知道要部署哪一個。請參閱我的問題中的「更新1」部分,我已經發布了所有這些文件夾的列表。如果可以,請通過查看該列表來查看我需要選擇的文件夾。謝謝。 – brilliant 2010-04-27 05:59:12

+1

您希望頂層的應該稱爲「斜紋」,包括其所有子文件夾。這是整個包裹。 – geoffspear 2010-04-27 10:59:13

+0

謝謝,Wooble !!! – brilliant 2010-04-27 12:45:05

1

不知道它(當然,用Google搜索),但AppEngine上提供fetch() function可以用來抓取網頁什麼斜紋。它也支持POST方法,例如登錄。

(我懷疑在AppEngine上斜紋作品,因爲AppEngine上提供數量有限的出於安全原因,Python庫。只是一個猜測,雖然)。

+0

你好Yaggo !!!!感謝您的答覆。是的,我知道這個獲取函數,但問題是我無法登錄,因爲我不熟悉所有需要知道的標題和其他規範的工作方式,當您嘗試登錄到網站時使用從「Google App Engine」運行的Python代碼。 – brilliant 2010-04-27 04:57:15

+0

Here(http://stackoverflow.com/questions/1899259/how-can-i-pass-my-id-and-my-password-to-a-website-in-python-using-google-app-engi )和這裏(http:// stackoverflow。com/questions/1914275/googles-app-engine-python-how-to-get-parameters-from-a-log-in-pages)我被建議使用斜紋作爲斜紋自動處理所有這些東西,不需要深入瞭解所有這些細節。這實際上是我如何知道斜紋。 – brilliant 2010-04-27 04:57:45

1

我相信你正在尋找一種方式來導入斜紋模塊插入應用程序-發動機。你必須弄清楚twill python文件的位置,或者如何獲得它們的源代碼包以便將它打包到你的網站中,但是看起來像導入第三方模塊可以做一些例外,見下文。

嘗試ZipImport遵循Google的網站herehere的指示。

從谷歌的第三方庫頁:

App Engine使用該功能的zipimport而不是標準實施的定製版本。它通常以通常的方式工作:將Zip存檔添加到sys.path,然後照常導入。除了以下例外: zipimport只能將存檔文件中存儲的模塊導入爲.py源文件。它不能導入以.pyc或.pyo文件存儲的模塊。 zipimport是用純Python實現的,並不使用本地代碼進行解壓縮(C代碼)。

+0

謝謝你,winfred,這個輸入。我將首先嚐試Wooble在這裏提出的方式。如果那個不行,我會訴諸你的。再次感謝。 – brilliant 2010-04-27 06:14:09

2

這裏是一個使用斜紋來運行谷歌搜索的例子,如果這有幫助。它顯示了使用斜紋和美麗一起來解析網頁:

>>> 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/[email protected]/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/[email protected]/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> 
>>> 
+0

非常感謝,amadain !!!!我會花一些時間來研究你的代碼。 – brilliant 2010-05-20 20:44:15

相關問題