Skip to main content
 首页 » 编程设计

python之将 showforms() 命令与 Twill 结合使用时出现解析错误

2025年05月04日7mengfanrong

我对这个程序的想法是有一个简单的(据说)脚本来监视现在几点,当它在特定时间范围内(例如早上 6 点到晚上 7 点)时,它会导航到 opendns.com 并阻止某些网站使用网页内容过滤功能。

我以为我会从简单的开始,只是弄清楚登录网站和阻止网站的命令,然后再担心时间的监控等。但遗憾的是,我也遇到了麻烦。

我正在使用 http://twill.idyll.org/但不确定这是否是个好主意。这是除了机械化之外我唯一能找到的(我找不到合适的文档,但也许我只是没有在正确的地方寻找)

这是我的代码(好吧,它还不是真正的代码。只是 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> 

请您参考如下方法:

是的,python 的斜纹 Material 并不是世界上最好的文档。我认为您基本上可以忘记“get_browser”这些东西。这样斜纹布的东西对我来说更清楚一点:

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()