【问题标题】:Parse Error while using Mechanize in Python在 Python 中使用 Mechanize 时解析错误
【发布时间】:2012-06-17 16:12:59
【问题描述】:

我正在尝试在 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。任何帮助将不胜感激

谢谢, 詹姆斯

【问题讨论】:

    标签: python forms mechanize parse-error


    【解决方案1】:

    在我看来,页面可能以 UTF-8 编码。 '&lt;!\xe2\x80\x94IE7 mode --\n &lt;' 将解码为 u'&lt;!—IE7 mode --\n &lt;'。也许那是一个 HTML 注释 &lt;!--,但 -- 已更改为

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-09-11
        • 2013-08-09
        • 2011-01-05
        • 2012-05-12
        • 1970-01-01
        • 1970-01-01
        • 2018-02-01
        • 2012-04-23
        相关资源
        最近更新 更多