使用Twill的showforms()命令时出现解析错误

2 投票
1 回答
703 浏览
提问于 2025-04-17 16:54

我这个程序的想法是写一个简单的脚本,监控当前时间,当时间在某个范围内(比如早上6点到晚上7点)时,它会自动打开opendns.com网站,并利用网站的内容过滤功能来屏蔽一些特定的网站。

我想先从简单的开始,先搞清楚怎么登录网站和屏蔽网站,等这些搞定后再考虑时间监控的问题。但可惜的是,我在这方面也遇到了麻烦。

我正在使用http://twill.idyll.org/,但不确定这是不是个好主意。这是我找到的唯一一个工具,除了mechanize(我找不到合适的文档,可能是我没有在对的地方找)

这是我的代码(其实还不算代码,只是一堆Python Shell的命令):

from twill import get_browser
from twill.commands import *

username = "username@email.com" # email for opendns
password = "thisisthepassword" # password for opendns
b = get_browser()

b.go("https://dashboard.opendns.com/")
b.showforms()

fv("2", "username", username)
fv("2", "password", password)
showforms()

submit("sign-in")

b.showforms()

b.go ("https://dashboard.opendns.com/settings/*MYNETWORKID*/content_filtering") # I replaced my network ID due to privacy reasons but this is basically the URL to the web content filtering page on OpenDNS for a network

b.showforms()

现在我的问题就出在这里。 在最后那个b.showforms()这行,我遇到了一个错误:

Traceback (most recent call last):
  File "<pyshell#43>", line 1, in <module>
    b.showforms()
  File "C:\Python27\lib\site-packages\twill-0.9-py2.7.egg\twill\browser.py", line 225, in showforms
    forms = self.get_all_forms()
  File "C:\Python27\lib\site-packages\twill-0.9-py2.7.egg\twill\browser.py", line 259, in get_all_forms
    global_form = self._browser.global_form()
  File "C:\Python27\lib\site-packages\twill-0.9-py2.7.egg\twill\other_packages\_mechanize_dist\_mechanize.py", line 446, in global_form
    return self._factory.global_form
  File "C:\Python27\lib\site-packages\twill-0.9-py2.7.egg\twill\utils.py", line 334, in get_global_form
    return self.factory.global_form
  File "C:\Python27\lib\site-packages\twill-0.9-py2.7.egg\twill\other_packages\_mechanize_dist\_html.py", line 521, in __getattr__
    self.forms()
  File "C:\Python27\lib\site-packages\twill-0.9-py2.7.egg\twill\other_packages\_mechanize_dist\_html.py", line 534, in forms
    self._forms_factory.forms())
  File "C:\Python27\lib\site-packages\twill-0.9-py2.7.egg\twill\other_packages\_mechanize_dist\_html.py", line 226, in forms
    raise ParseError(exc)
ParseError: <unprintable ParseError object>

1 个回答

1

是的,关于Python的twill库,文档写得并不是很好。我觉得你可以基本上不考虑“get_browser”这个内容。对于我来说,twill的用法可以这样理解:

import twill.commands as twill

username = "username@email.com" # email for opendns
password = "thisisthepassword" # password for opendns

twill.go("https://dashboard.opendns.com/")
twill.showforms()

twill.fv("2", "username", username)
twill.fv("2", "password", password)
twill.showforms()

twill.submit("sign-in")

twill.showforms()

twill.go ("https://dashboard.opendns.com/settings/*MYNETWORKID*/content_filtering") # I replaced my network ID due to privacy reasons but this is basically the URL to the web content filtering page on OpenDNS for a network

twill.showforms()

撰写回答