如何将twill和python组合成一个可以在“googleappengine”上运行的代码?

2024-10-02 06:36:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经在我的电脑上安装了twill(之前安装了python2.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”文件夹中的文件:E:\tmp\twill-0.9\测试.斜纹 我在命令提示符下运行这个文件:pythontwillsh测试.斜纹在

现在,我也安装了“谷歌应用引擎”中的“谷歌应用引擎SDK”,并且已经使用了一段时间。在

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

^{pr2}$

这段代码帮助我将短语“无人检查垃圾邮件重复”转换为md5摘要。在

现在,我如何将这两段代码组合成一个可以在“googleappengine”上运行的python脚本?在

比方说,我希望我的代码从“googleappengine”登录到一个网站,转到该网站的另一个页面,记录它的HTML代码(这是我的twill代码所做的),然后将这个HTML代码转换成md5摘要(这是我的第二个代码所做的)。那么,如何将这两个代码组合成一个python代码呢?在

我想,应该通过进口斜纹布来完成,但是怎么能做到呢?一个python代码——由“googleappengine”运行的代码——能从互联网上的某个地方导入twill吗?或者“谷歌引擎已经安装了”?在


更新1:

(这是我对伍布尔回答的回应)

这是包含__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\u packages\\u mechanize_dist

E:\twill-0.9\build\lib\twill\other\u包

E:\twill-0.9\build\lib\twill

E:\twill-0.9\twill\extensions\match\parse

E:\twill-0.9\twill\扩展

E:\twill-0.9\twill\other\u packages\\u mechanize_dist

E:\twill-0.9\twill\other\u软件包

E:\twill-0.9\twill


Tags: 文件代码引擎build文件夹url列表网站
3条回答

下面是一个使用twill运行google搜索的例子,如果这有帮助的话。它显示了使用twill和beauthoulsoup一起解析网页:

>>> 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做什么(好吧,google),但是AppEngine提供了^{} function可以用来获取网页。它还支持POST方法,例如登录。在

(我怀疑twill能在AppEngine中工作,因为AppEngine由于安全原因,python库有限。不过,只是猜测一下。)

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

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

相关问题 更多 >

    热门问题