使用 Python 中的 Twill 打开链接:“ “模块”对象没有属性“Popen”; ”它是什么?

发布于 2024-09-16 22:15:51 字数 3729 浏览 8 评论 0 原文

我已经下载并安装了 Python 2.5.4 在我的计算机上(我的操作系统是 Windows XP),下载“Goggle App Engine 软件开发套件” 并用 Python 创建了我的第一个应用程序,该应用程序是一个名为 helloworld 包含一个同名的小 python 文件 (helloworld.py)。以下是该小文件的内容:


print 'Content-Type: text/plain'
print ''
print 'Hello, world!' 

每当我使用 “Goggle App Engine 软件开发套件”,我的浏览器 (FireFox) 总是向我显示一个白色窗口,上面写着 Hello, world! > 写在里面。

然后我下载了 Twill 并将其解压到 helloworld< /em> 目录。 正确安装 Twill,我能够从 Twill shell 执行一些小命令。例如,我可以通过某个链接转到网页:

alt text

然后我想直接从Python(即通过使用 Python 中的 Twill。)以下是 Twill 文档页面的内容描述如下


twill 的 Python API

使用 TwillBrowser 制作扩展

twill 本质上是 mechanize 包周围的一个薄壳。所有斜纹命令都在commands.py 文件中实现,pyparsing 负责解析输入并将其转换为Python 命令的工作(请参阅parse.py)。交互式 shell 工作和 readline 支持是通过 cmd 模块(来自标准 Python 库)实现的。

在 Python 中使用斜纹

从 Python 中使用斜纹有两种相当简单的方法。 (它们彼此兼容,因此您无需在它们之间进行选择;只需使用合适的即可。)

第一个是简单地导入commands.py 中的所有命令并直接从Python 使用它们。例如,

from twill.commands import *
go("http://www.python.org/")
showforms()

这具有非常简单的优点,并且直接与命令参考中记录的命令集相关联。


所以我决定使用这种方式。我删除了 helloworld.py 之前的内容并为其提供了新内容:


from twill.commands import *
go("http://www.python.org/")
showforms()

但是当我尝试使用 “Goggle App Engine 软件开发工具包”,我的浏览器,而不是描绘www.python.org 网站的内容给我一条错误消息:'module' object has no attribute 'Popen' :

alt text

请看一下整个页面此处

以下是该页面的最后三行:


: 'module' 对象没有属性 'Popen'

  args = ("'module' object has no attribute 'Popen'",)

  message = "'module' object has no attribute 'Popen'" 

请任何人向我解释一下这个 Popen 属性的含义以及我在这里做错了什么?

预先感谢大家。


更新1

(此更新是我对下面leoluk提供的第二个答案的回应)

你好,leoluk!

我尝试这样做:

config use_tidy 0
from twill.commands import *
go("http://www.python.org/")

但没有成功。我收到此错误消息:(

<type 'exceptions.SyntaxError'>: invalid syntax (helloworld.py, line 1)
      args = ('invalid syntax', (r'E:\helloworld\helloworld.py', 1, 15, 'config use_tidy 0\n'))
      filename = r'E:\helloworld\helloworld.py'
      lineno = 1
      message = ''
      msg = 'invalid syntax'
      offset = 15
      print_file_and_line = None
      text = 'config use_tidy 0\n'

您可以在此处<查看整个页面/strong>)

您知道这意味着什么以及出了什么问题吗?

I have downloaded and installed Python 2.5.4 on my computer (my OS is Windows XP), downloaded “Goggle App Engine Software Development Kit” and created my first application in Python, which was a directory named helloworld that contained a small python file with the same name (helloworld.py). Here are the contents of that small file:


print 'Content-Type: text/plain'
print ''
print 'Hello, world!' 

Whenever I ran this application locally on my computer with “Goggle App Engine Software Development Kit”, my browser (FireFox) always showed me a white window with Hello, world! written in it.

Then I downloaded Twill and unpacked it into helloworld directory. Having installed Twill properly, I was able to execute some small commands from Twill shell. For example, I could turn to a web page by some link:

alt text

Then I wanted to perform the same operation directly from Python (i.e. by means of using Twill from Python.) Here is what the Twill documentation page says about it:


twill's Python API

Using TwillBrowser Making extensions

twill is essentially a thin shell around the mechanize package. All twill commands are implemented in the commands.py file, and pyparsing does the work of parsing the input and converting it into Python commands (see parse.py). Interactive shell work and readline support is implemented via the cmd module (from the standard Python library).

Using twill from Python

There are two fairly simple ways to use twill from Python. (They are compatible with each other, so you don't need to choose between them; just use whichever is appropriate.)

The first is to simply import all of the commands in commands.py and use them directly from Python. For example,

from twill.commands import *
go("http://www.python.org/")
showforms()

This has the advantage of being very simple, as well as being tied directly to the documented set of commands in the commands reference.


So I decided to use this way. I deleted the previous contents of helloworld.py and gave it the new contents:


from twill.commands import *
go("http://www.python.org/")
showforms()

But when I tried to run that file on my computer with “Goggle App Engine Software Development Kit”, my browser, instead of depicting the contents of www.python.org web site, gives me an error message: 'module' object has no attribute 'Popen' :

alt text

Please, take a look at the whole page here.

Here are the last three lines of that page:


: 'module' object has no attribute 'Popen'

  args = ("'module' object has no attribute 'Popen'",)

  message = "'module' object has no attribute 'Popen'" 

Can anybody, please, explain to me what this Popen attribute is all about and what I am doing wrong here?

Thank you all in advance.


Update 1

(this update is my response to the second answer provided below by leoluk)

Hello, leoluk!!!

I tried doing it this way:

config use_tidy 0
from twill.commands import *
go("http://www.python.org/")

but it didn't work. I received this error message:

<type 'exceptions.SyntaxError'>: invalid syntax (helloworld.py, line 1)
      args = ('invalid syntax', (r'E:\helloworld\helloworld.py', 1, 15, 'config use_tidy 0\n'))
      filename = r'E:\helloworld\helloworld.py'
      lineno = 1
      message = ''
      msg = 'invalid syntax'
      offset = 15
      print_file_and_line = None
      text = 'config use_tidy 0\n'

(You can see the whole page HERE)

Do You have any idea what it means and what went wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眼眸里的那抹悲凉 2024-09-23 22:15:51

整洁的程序做得很好
从损坏中生成正确的 HTML,
损坏的、邪恶的网页。默认情况下,
斜纹将通过整齐排列页面
在处理它们之前。这是由
默认值,因为 Python 库
解析 HTML 的能力非常差
处理不正确的 HTML,并将
经常返回不正确的结果
“现实世界”网页。

要禁用此功能,请将配置 do_run_tidy 设置为 0。

The tidy program does a nice job of
producing correct HTML from mangled,
broken, eeevil Web pages. By default,
twill will run pages through tidy
before processing them. This is on by
default because the Python libraries
that parse HTML are very bad at
dealing with incorrect HTML, and will
often return incorrect results on
"real world" Web pages.

To disable this feature, set config do_run_tidy 0.

中性美 2024-09-23 22:15:51

您无法使用 Google App 引擎中的任何内容。 Twill 使用谷歌应用程序引擎上不可用的东西来工作。因此应用程序引擎不完全支持斜纹。

值得注意的是,该代码尝试调用外部命令 tidy,并且在应用引擎中调用外部命令不起作用。

you can't use anything in the Google App engine. Twill uses stuff not available on google app engine to work. So twill is not fully supported by app engine.

notably, the code is trying to call on an external command, tidy, and calling external commands in app engine doesn't work.

邮友 2024-09-23 22:15:51

我认为你应该直接使用 mechanize 。 Twill 以 Google App Engine 不支持的方式与系统通信。

import mechanize

browser = mechanize.Browser()

browser.open('http://www.python.org')

for f in browser.forms():
    print f # you'll have to extend it

I think you should use mechanize directly. Twill communicates with the system in a way that's not supported by Google App Engine.

import mechanize

browser = mechanize.Browser()

browser.open('http://www.python.org')

for f in browser.forms():
    print f # you'll have to extend it
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文