Skip to main content
 首页 » 编程设计

python Mechanize FormNotFoundError

2025年05月04日8luoye11

我正在研究 Mechanize 以获取表单元素

import mechanize 
br = mechanize.Browser() 
br.set_handle_robots(False) 
br.open("http://www.bnm.gov.my/index.php?ch=12&pg=622") 
br.select_form(name="Rates") 

但这是抛出错误:

FormNotFoundError: no form matching name 'Rates' 

虽然有

<form onsubmit="return validate();" method="get" action="index.php" name="Rates"> 

有人可以帮忙吗

提前致谢

请您参考如下方法:

尝试使用 nr 参数选择表单:

select_form(self, name=None, predicate=None, nr=None) 

我猜那个页面只有 1 个表单,所以试试 nr=0。

如果页面中没有表单,则可能意味着它是使用 Javascript 添加的。而且,在那种情况下, Mechanize 是不够的。您将不得不使用 Selenium 或 Spynner。