【问题标题】:How to combine twill and python into one code that could be run on "Google App Engine"?如何将 twill 和 python 组合成一个可以在“Google App Engine”上运行的代码?
【发布时间】:2010-04-26 22:19:50
【问题描述】:

我已经在我的电脑上安装了斜纹布(之前安装了 Python 2.5)并且最近一直在使用它。

Python安装在我电脑的C盘:C:\Python25

而斜纹文件夹(“twill-0.9”)位于此处:E:\tmp\twill-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”(当然,在使用此代码之前我首先需要将“我的登录”替换为我的真实登录名,“我的密码”替换为我的真实密码,“某个网站的登录页面URL”和“某些URL该网站的其他页面”,其中包含该网站的真实 URL,第 2 位是该网站上用作该网站登录页面上的登录表单的表单编号)

我将这段代码存储在“twill-0.9”文件夹中的“test.twill”文件中:E:\tmp\twill-0.9\test.twill 我从命令提示符运行此文件:python twill-sh 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 代码(这就是我的斜纹代码所做的),然后将此 HTML 代码转换为它的 md5 摘要(这就是我的第二个代码所做的)。那么,如何将这两个代码组合成一个 python 代码呢?

我想,应该通过导入斜纹来以某种方式完成,但是怎么做呢? python 代码——由“Google App Engine”运行的代码——可以从互联网上的某个地方导入斜纹布吗?或者,也许斜纹已经安装在“Google App Engine”上?


更新 1:

(此更新是我对 Wooble 的回答)

这是包含__init__.py 文件的所有文件夹的列表(在我的“twill-0.9”文件夹中)。 (此列表中的某些文件夹位于此列表中也提到的其他文件夹中):

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

【问题讨论】:

    标签: python google-app-engine import twill


    【解决方案1】:

    如果有帮助,这里有一个使用斜纹布运行 google 搜索的示例。它展示了使用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>
    >>>  
    

    【讨论】:

      【解决方案2】:

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

      查看 twill Google Code 项目,似乎 twill 在包中包含其依赖项(pyparsing、mechanize 等),因此您可能不需要包含其他任何内容。

      【讨论】:

      • 你好伍布尔!!!!非常感谢您的宝贵回复!我刚刚查看了我的“twill-0.9”文件夹(我从下载的 twill .zip 包中提取的那个),发现其中有几个文件夹包含一个 `__init__.py 文件。所以,现在我有点困惑,因为我不知道要部署哪个。请参阅我的问题中的“更新 1”部分,我已在其中发布了所有这些文件夹的列表。如果可以,请通过查看该列表告诉我我需要选择哪个文件夹。谢谢。
      • 你想要顶层的,应该叫做“twill”,包括它的所有子文件夹。这是整个包。
      • 你好,伍布尔!!!很抱歉再次打扰您,但似乎有些事情不适合这种方式。如果您有时间并愿意提供帮助,请查看此页面,其中我提出了一个关于斜纹和 python 的新问题:stackoverflow.com/questions/3621432/…
      【解决方案3】:

      不知道 twill 做什么(好吧,谷歌搜索),但 AppEngine 提供了fetch() function,可用于获取网页。它还支持 POST 方法,例如用于登录。

      (我怀疑 twill 是否适用于 AppEngine,因为出于安全原因,AppEngine 提供的 Python 库有限。不过,只是猜测。)

      【讨论】:

      • 你好亚戈!!!!感谢您的答复。是的,我知道这个 fetch 功能,但问题是我无法使用它登录,因为我不熟悉标题的所有工作方式以及您在尝试登录网站时需要知道的其他规范使用从“Google App Engine”运行的 python 代码。
      • 这里 (stackoverflow.com/questions/1899259/…) 和这里 (stackoverflow.com/questions/1914275/…) 有人建议我使用斜纹布,因为斜纹布会自动完成所有这些操作,因此我不需要深入研究所有这些细节。我就是这样认识斜纹布的。
      【解决方案4】:

      我相信您正在寻找一种将斜纹模块导入 App-Engine 的方法。您必须弄清楚斜纹 python 文件的位置或如何获取它们的源包以将其与您的网站打包,但看起来导入 3rd 方模块可以完成一些例外,见下文。

      按照 Google 网站 herehere 的说明尝试 ZipImport

      来自 Google 的第三方库页面:

      App Engine 使用自定义版本的 zipimport 功能,而不是标准实现。它通常以通常的方式工作:将 Zip 存档添加到 sys.path,然后照常导入。除了这些例外: zipimport 只能将存储在存档中的模块导入为 .py 源文件。它无法导入存储为 .pyc 或 .pyo 文件的模块。 zipimport纯Python实现,不使用native代码解压(C代码)。

      【讨论】:

      • 感谢您,winfred,您的意见。我将首先尝试 Wooble 建议的方式。如果那个不起作用,我会求助于你的。再次感谢。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-31
      • 1970-01-01
      • 2019-12-15
      相关资源
      最近更新 更多