题
我刚刚安装成功 斜纹 在“StackOverflow”一位非常支持的成员的帮助下在我的计算机上(你可以查看 这里)并尝试运行斜纹文档页面上的简单示例之一(您可以看到该页面 这里)。这是这个例子:
(来源: 俄罗斯国家报)
假设我的用户名是 www.slash.org 是 林克西 我的密码是 哺乳动物. 。当我尝试在 Python 提示符中输入该示例代码时,我只能键入并输入代码的第一行,因为当我单击“Enter”开始新行时,我会立即收到一些错误消息:
(来源: 俄罗斯国家报)
当我尝试将此代码输入终端时,也会发生同样的情况:
(来源: 俄罗斯国家报)
我想我在这里错过了一些基础知识。也许,我需要创建一个包含该代码的文件,然后以某种方式运行该文件,但我真的不知道需要在哪里创建该文件以及使用什么扩展名。
有人可以帮我解决这个问题吗?
解决方案
您需要在 twill shell 中运行这些命令,而不是 python shell
D:\tmp\twill-0.9>python 斜纹sh
-= Welcome to twill! =-
current page: *empty page*
>>
当你看到时,你可以输入斜纹命令 >>
或者 twill-sh 文件中只有 2 行
import twill.shell
twill.shell.main()
您只需将这两行复制粘贴到 python 提示符中即可。
>>> 导入斜纹.shell
>>> 斜纹.shell.main()
-= Welcome to twill! =-
current page: *empty page*
>> ?
Undocumented commands:
======================
add_auth fa info save_html title
add_extra_header find load_cookies setglobal url
agent follow notfind setlocal
back formaction redirect_error show
clear_cookies formclear redirect_output show_cookies
clear_extra_headers formfile reload show_extra_headers
code formvalue reset_browser showforms
config fv reset_error showhistory
debug get_browser reset_output showlinks
echo getinput run sleep
exit getpassword runfile submit
extend_with go save_cookies tidy_ok
current page: *empty page*
>>
其他提示
这会做到:
>>> import twill.commands
>>> import re
>>>
>>> class browser:
... def __init__(self, url="http://www.slashdot.org"):
... 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.b.clear_cookies()
... self.url=url
... def slashdot(self, loginName, passwd):
... self.b.go(self.url)
... f = self.b.get_form("1")
... regexp = re.compile("Log In")
... link = self.b.find_link(regexp)
... if link:
... self.b.follow_link(link)
... f=self.b.get_form("2")
... f["unickname"] = loginName
... f["upasswd"] = passwd
... self.a.fv("2", "userlogin", "")
...
>>>
>>> t=browser()
>>> t.slashdot("username", "password")
==> at http://slashdot.org/
==> at http://slashdot.org/my/login
forcing read-only form field to writeable
>>>
如果您使用Beautifutoup来解析HTML,它也会更容易
角度括号并不是要从字面上输入;他们只是在那里表明您应该输入一个值。尝试
setlocal username lynxye
反而。
这不是在提示中使用的方法。
您需要将钥匙值对包装在括号中。像,
setLocal('uname','xyz')
setLocal('密码','123')
去('http://abc.org')
FV('1','foo','bar')
不隶属于 StackOverflow