2012-07-25 8 views
5

私はPython 2.7とMechanize 2.5を使用しています。私はselect_form()メソッドを使用しようとしていますが、私は次のエラーを取得しています:Python Mechanize select_form() - ParseError:SELECTの外側のOPTION

File "C:\Python27\lib\site-packages\mechanize\_mechanize.py", line 499, in select_form 
    global_form = self._factory.global_form 
    File "C:\Python27\lib\site-packages\mechanize\_html.py", line 544, in __getattr__ 
    self.forms() 
    File "C:\Python27\lib\site-packages\mechanize\_html.py", line 557, in forms 
    self._forms_factory.forms()) 
    File "C:\Python27\lib\site-packages\mechanize\_html.py", line 237, in forms 
    _urlunparse=_rfc3986.urlunsplit, 
    File "C:\Python27\lib\site-packages\mechanize\_form.py", line 845, in ParseResponseEx 
    _urlunparse=_urlunparse, 
    File "C:\Python27\lib\site-packages\mechanize\_form.py", line 982, in _ParseFileEx 
    fp.feed(data) 
    File "C:\Python27\lib\site-packages\mechanize\_form.py", line 759, in feed 
    _sgmllib_copy.SGMLParser.feed(self, data) 
    File "C:\Python27\lib\site-packages\mechanize\_sgmllib_copy.py", line 110, in feed 
    self.goahead(0) 
    File "C:\Python27\lib\site-packages\mechanize\_sgmllib_copy.py", line 144, in goahead 
    k = self.parse_starttag(i) 
    File "C:\Python27\lib\site-packages\mechanize\_sgmllib_copy.py", line 302, in parse_starttag 
    self.finish_starttag(tag, attrs) 
    File "C:\Python27\lib\site-packages\mechanize\_sgmllib_copy.py", line 347, in finish_starttag 
    self.handle_starttag(tag, method, attrs) 
    File "C:\Python27\lib\site-packages\mechanize\_sgmllib_copy.py", line 387, in handle_starttag 
    method(attrs) 
    File "C:\Python27\lib\site-packages\mechanize\_form.py", line 736, in do_option 
    _AbstractFormParser._start_option(self, attrs) 
    File "C:\Python27\lib\site-packages\mechanize\_form.py", line 481, in _start_option 
    raise ParseError("OPTION outside of SELECT") 
ParseError: OPTION outside of SELECT 

これは私のコードです:

cj = cookielib.LWPCookieJar() 
br = mechanize.Browser() 
br.set_cookiejar(cj) 
br.set_handle_equiv(True) 
br.set_handle_gzip(True) 
br.set_handle_redirect(True) 
br.set_handle_referer(True) 
br.set_handle_robots(False) 
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1) 
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')] 
br.open("website_url_which_i_will_not_share") 
br.select_form(nr=0) 

以下は、Webページ上のHTMLのフォームセクションであります私は

<html lang="en-us" xml:lang="en-us" xmlns="http://www.w3.org/1999/xhtml"> 
    <head> I omitted this section </head> 
    <body class="login"> 
     <div id="container"> 
      <div id="header" style="background-color: #13397A;"> 
      <div id="content" class="colM"> 
       <div id="content-main"> 
        <form id="login-form" method="post" action="/admin/"> 
         <div style="display:none"> 
          <input type="hidden" value="8a689f2e3d215a3465f1bb66e037d1a5" name="csrfmiddlewaretoken"> 
         </div> 
         <div class="form-row"> 
          <label class="required" for="id_username">Username:</label> 
          <input id="id_username" type="text" maxlength="30" name="username"> 
         </div> 
         <div class="form-row"> 
          <label class="required" for="id_password">Password:</label> 
          <input id="id_password" type="password" name="password"> 
          <input type="hidden" value="1" name="this_is_the_login_form"> 
          <input type="hidden" value="/admin/" name="next"> 
         </div> 
         <div class="submit-row"> 
          <label>&nbsp;</label> 
          <input type="submit" value="Log in"> 
         </div> 
        </form> 
        <script type="text/javascript"> 
       </div> 
       <br class="clear"> 
      </div> 
      <div id="footer"></div> 
     </div> 
     <script type="text/javascript"> 
    </body> 
</html> 

を開いたことを私はstackoverflowの上やGoogleでこれを研究してきたが、私は同様の質問や、このエラーのも、説明を見つけることができません。

このエラーの意味を教えてくれたら、ここで何が間違っているのかを知る手助けができたら、私は非常に感謝しています。

おかげ

編集:私は、フォームの提出をたくさんやっていると、すべてのサイトはこの1つを除いて正常に動作します。これはデータベースAPIで、データのスクラップを試みています。

+0

問題を再現できるようにするには、完全なページソースがなくても、実際には不可能です。 –

+0

私に教えてくれてありがとう、私はすべてのhtml(headタグを除く)を含めるように私の質問を編集しました – camelCase

+0

ウェブブラウザで入手したソースと機械化されたリクエストはデバッグの目的で異なるかもしれません 'print br .forms'を呼び出してからselect_formを呼び出します。おそらく、デフォルトのパーサーは無効なHTMLに対処できないでしょう( ''は ''でしょうか?)。 –

答えて

2

私は同じ問題を抱えていた(残念ながらまだそれを解決していない)、と私はこのコードの面白い作品を見つけ、それはこれがHTTP_RESPONSEをオーバーライドする必要がありますhttp://comments.gmane.org/gmane.comp.python.wwwsearch.general/1991

import mechanize 
from BeautifulSoup import BeautifulSoup 

class SanitizeHandler(mechanize.BaseHandler): 
    def http_response(self, request, response): 
     if not hasattr(response, "seek"): 
      response = mechanize.response_seek_wrapper(response) 
     #if HTML used get it though a robust Parser like BeautifulSoup 

     if response.info().dict.has_key('content-type') and ('html' in response.info().dict['content-type']): 
      soup = BeautifulSoup(response.get_data()) 
      response.set_data(soup.prettify()) 
     return response 

br = mechanize.Browser() 
br.add_handler(SanitizeHandler()) 

# Now you get good HTML 

から

を助けるかもしれませんメソッドとあなたのHTMLを "クリーン"。

+0

投稿されたソースは死んでいます。なぜこれが解決策なのか、さらに詳しい情報を教えてください。 –

関連する問題