我已经在计算机上安装了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代码(我只能在登录后才可以访问),然后将其存储在名为“结果的文件中。 txt”(当然,在使用此代码之前,我首先需要用我的真实密码替换“我的登录”,“我的密码”,“某些网站的登录页面URL”和“来自其他页面的URL该网站“具有该网站的真实URL,以及该网站上的表格数量的2号,该表单被用作该网站的登录页面上的登录表格)

该代码我存储在我的“ twill-0.9”文件夹中的“ test.twill”文件中:斜纹

现在,我还从“ Google App Engine”安装了“ Google App Engine SDK”,并且还使用了一段时间。

例如,我一直在使用此代码:

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

该代码有助于我将“没有人检查垃圾邮件重复”转换为MD5摘要。

现在,如何将这两个代码放在一个可以在“ Google App Engine”上运行的Python脚本中?

假设我希望我的代码从“ Google App Engine”登录到网站,转到该网站上的另一个页面,记录其HTML代码(这是我的Twill代码所做的),而不是将此HTML代码转换为MD5 Digest (这就是我的第二个代码所做的)。那么,如何将这两个代码组合到一个Python代码中?

我想,应该以某种方式导入Twill,但是如何完成? Python代码(由“ Google App Engine”运行的代码可以从Internet上的某个地方导入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 elets_packages _mechanize_dist

e: twill-0.9 build lib twill shot_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

有帮助吗?

解决方案

要在App Engine Projects中使用第三方库,您只需要在部署时将它们包含在应用程序中。复制斜纹文件夹(包含一个文件夹 __init__.py)进入应用程序的文件夹并部署它。

查看Twill Google代码项目,看来Twill在包装中包含其依赖关系(pyparsing,机械化等),因此您可能不需要包含其他内容。

其他提示

这是使用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>
>>>  

不知道Twill的作用(好吧,谷歌搜索),但是Appengine提供了 fetch() 功能 可用于获取网页。它还支持登录的邮政方法。

(我怀疑Twill在Appengine中工作,因为Appengine出于安全原因有限可用的Python库。

我相信您正在寻找一种将Twill模块导入App-Engine的方法。您必须弄清楚Twill Python文件所在的位置,或者如何获取它们的源包来与您的网站打包,但是看起来可以在一些例外情况下导入第三方模块,请参见下文。

尝试 Zipimport 遵循Google网站的指示 这里这里.

从Google的第三方库页面:

App Engine使用Zipimport功能的自定义版本,而不是标准实现。它通常以通常的方式工作:将ZIP存档添加到SYS.Path中,然后像往常一样导入。除这些例外:Zipimport只能导入存储在存档中的.PY源文件中的模块。它无法导入存储为.pyc或.pyo文件的模块。 Zipimport在Pure Python中实现,并且不使用本机代码进行减压(C代码)。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top