python - 使用 Python 中的 Twill 打开链接 : "' module' object has no attribute 'Popen' "What is it?

标签 python google-app-engine popen twill

我已经下载并安装了 Python 2.5.4 在我的电脑上(我的操作系统是 Windows XP),已下载 “Goggle App Engine Software Development Kit” 并在 Python 中创建了我的第一个应用程序,该应用程序是一个名为 helloworld 的目录,其中包含一个同名的小 python 文件 (helloworld .py)。以下是该小文件的内容:


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

每当我使用 “Goggle App Engine Software Development Kit” 在我的计算机上本地运行此应用程序时,我的浏览器 (FireFox) 总是显示一个白色窗口,其中写着 Hello, world!

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

alt text

然后我想直接从 Python 执行相同的操作(即通过使用 Python 的 Twill。)这是 Twill documentation page says about it 的内容:


twill 的 Python API

使用TwillBrowser制作扩展

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

在 Python 中使用 twill

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

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

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

这样做的优点是非常简单,并且直接绑定(bind)到命令引用中记录的命令集。


所以我决定采用这种方式。我删除了 helloworld.py 之前的内容并赋予了它新的内容:


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

但是当我尝试使用 “Goggle App Engine Software Development Kit” 在我的计算机上运行该文件时,我的浏览器没有显示 www.python.org 网站的内容,而是给我一条错误消息:'module' object has no attribute 'Popen':

alt text

请看一下整个页面 here

这是该页面的最后三行:


: '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'

(可以看到整个页面 HERE )

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

最佳答案

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.

关于python - 使用 Python 中的 Twill 打开链接 : "' module' object has no attribute 'Popen' "What is it?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3621432/

相关文章:

python - MongoDB:如何确定特定值不存在

python - ElasticSearch地理距离查询

python - 无法为 Wagtail 设置 Elasticsearch

google-app-engine - ndb 在没有父级的情况下按 ID 检索实体键

python - 部署到 App Engine 标准环境时如何修复 'ImportError: cannot import name base'?

Python打印对象设计

python - 过滤 Python、Google App Engine 中的所有列

python - 选择 python popen process.kill() 或 process.terminate() 的更优雅的方法

Python - 使用 Popen 执行具有多个条件的查找

python - 如何通过python执行shell脚本