如何将twill和python结合成可以在“Google App Engine”上运行的代码?
我在我的电脑上安装了twill(之前安装了Python 2.5),最近一直在使用它。
Python安装在我电脑的C盘:C:\Python25
而twill文件夹(“twill-0.9”)位于这里:E:\tmp\twill-0.9
这是我在twill中使用的代码:
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”的文件中(当然,在使用这段代码之前,我需要把“my login”替换成我的真实登录名,把“my password”替换成我的真实密码,把“某个网站的登录页面URL”和“该网站的某个其他页面的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()
这段代码帮助我把短语“Nobody inspects the spammish repetition”转换成md5摘要。
现在,我该如何把这两段代码合并成一个可以在“Google App Engine”上运行的Python脚本呢?
假设我想让我的代码从“Google App Engine”登录一个网站,去该网站的另一个页面,记录它的HTML代码(这就是我的twill代码的功能),然后把这个HTML代码转换成md5摘要(这就是我的第二段代码的功能)。那么,我该如何把这两段代码合并成一个Python代码呢?
我想,这应该通过导入twill来实现,但具体该怎么做呢?在“Google App Engine”上运行的Python代码能从互联网上导入twill吗?或者,也许twill已经在“Google App Engine”上安装好了?
更新1:
(这个更新是我对Wooble回答的回应)
这是我“twill-0.9”文件夹中所有包含__init
__.py文件的文件夹列表。(这个列表中的一些文件夹位于其他文件夹内,这些文件夹也在列表中提到):
E:\twill-0.9\build\lib\twill\extensions\match_parse
E:\twill-0.9\build\lib\twill\extensions
E:\twill-0.9\build\lib\twill\other_packages\_mechanize_dist
E:\twill-0.9\build\lib\twill\other_packages
E:\twill-0.9\build\lib\twill
E:\twill-0.9\twill\extensions\match_parse
E:\twill-0.9\twill\extensions
E:\twill-0.9\twill\other_packages\_mechanize_dist
E:\twill-0.9\twill\other_packages
E:\twill-0.9\twill
4 个回答
我想你是在寻找一种方法,把twill模块导入到App Engine中。你需要弄清楚twill的Python文件在哪里,或者如何获取它们的源代码包,然后把它打包到你的网站里。不过,看起来导入第三方模块是可以做到的,只是有一些例外,下面会详细说明。
你可以尝试按照谷歌网站上的说明使用ZipImport,具体可以参考这里和这里。
来自谷歌第三方库页面的信息:
App Engine使用了一个定制版的zipimport功能,而不是标准的实现。它的工作方式通常是这样的:把Zip压缩包添加到sys.path中,然后像往常一样导入。但有以下几点例外: zipimport只能导入存储在压缩包中的.py源文件,不能导入存储为.pyc或.pyo文件的模块。 zipimport是用纯Python实现的,不使用本地代码进行解压(也就是C语言代码)。
这里有一个使用twill来进行谷歌搜索的例子,希望对你有帮助。这个例子展示了如何将twill和beautifulsoup一起使用来解析网页:
>>> 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>
>>>
要在App Engine项目中使用第三方库,你只需要在部署应用时把它们包含进去。把包含有__init__.py
的twill文件夹复制到你的应用文件夹里,然后进行部署。
从twill的Google Code项目来看,twill似乎已经把它需要的其他库(比如pyparsing、mechanize等)都包含在内了,所以你可能不需要再额外添加其他东西。