Skip to main content
 首页 » 编程设计

python之在 Python 中使用 Mechanize 时出现解析错误

2025年05月04日10zdz8207

我正尝试在 Dell Kace 票务页面上选择一个表格,但出现解析错误。我正在用 python 编程并且一直在使用 mechanize。我成功地能够登录到该站点。我听说您可以使用 Beautiful soup 等 html 清洁器来解决此问题,但这些似乎都不起作用。

br = mechanize.Browser() #have tried the various html cleaner options in mechanize 
cj = cookielib.LWPCookieJar() 
br.set_cookiejar(cj) 
br.set_handle_equiv(True) 
br.set_handle_redirect(True) 
br.set_handle_referer(True) 
br.set_handle_robots(False) 
br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US;   
rv:1.9.0.1)Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]  
.... 
url_ticket = 'http://kace-server/adminui/ticket.php?ID=%d' %(box1[sel+1]) 
url_org1 = "http://kace-server/common/switch_to_org.php?org=1" 
br.open(url_org1) 
br.open(url_ticket) 
br.select_form(name="ticket_form")   
br.form['fields[owner_filter]']=current_user[0] 
br.submit() 

程序在 br.select_form 行失败并出现以下错误

line 39, in assign 
  br.select_form(name="ticket_form") 
.... 
File "C:\Python27\lib\site-packages\mechanize-0.2.5-py2.7.egg\mechanize\_form.py",  
line 760, in feed raise ParseError(exc) 
ParseError: expected name token at '<!\xe2\x80\x94IE7 mode --\n  <' 

我在 html 中搜索了 '!\xe2... 字符串,但没有找到。我也为 select_form 尝试过 nr=0。任何帮助将不胜感激

谢谢, 詹姆斯

请您参考如下方法:

“\xe2\x80\x94”是字符“—”(不是“-”!)的 utf-8 编码形式。看起来它是 html 中的错字(或者一些使用 msword 作为 html 编辑器的假人?),应该是“