[Mechanize.ClientForm] double reading from urllib2.urlopen

Discussion in 'Python' started by tiktak.hodiki@gmail.com, Jan 7, 2009.

  1. Guest

    Hello, folks!
    I use mechanize.clientform to parse HTML-forms. I preliminary check
    response and call response.read().find("..."). But when it's taken to
    ClientForm.ParseResponse, it can't parse because of response.read() is
    zero-length text. The problem is that ClientForm.ParseResponse is not
    taken text of response, only object.

    Example:

    import urllib
    from ClientForm import ParseResponse
    response = urllib.urlopen("http://yandex.ru")
    if -1 != response.read().find("foobar"):
    pass
    form = ParseResponse(response)[1] <-- there is exception IndexError
     
    , Jan 7, 2009
    #1
    1. Advertising

  2. MRAB Guest

    wrote:
    > Hello, folks!
    > I use mechanize.clientform to parse HTML-forms. I preliminary check
    > response and call response.read().find("..."). But when it's taken to
    > ClientForm.ParseResponse, it can't parse because of response.read() is
    > zero-length text. The problem is that ClientForm.ParseResponse is not
    > taken text of response, only object.
    >
    > Example:
    >
    > import urllib
    > from ClientForm import ParseResponse
    > response = urllib.urlopen("http://yandex.ru")
    > if -1 != response.read().find("foobar"):
    > pass
    > form = ParseResponse(response)[1] <-- there is exception IndexError
    >

    It might be that read() is consuming the data, so there's none remaining
    for the second read(). Try:

    response = urllib.urlopen("http://yandex.ru")
    text = response.read()
    if "foobar" in text: # preferred to find()
    pass
    form = ParseResponse(text)[1]
     
    MRAB, Jan 7, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sydex
    Replies:
    12
    Views:
    6,645
    Victor Bazarov
    Feb 17, 2005
  2. Xu, C.S.
    Replies:
    5
    Views:
    515
    John J. Lee
    Sep 17, 2003
  3. Chris
    Replies:
    0
    Views:
    1,108
    Chris
    Jul 10, 2005
  4. Brian D
    Replies:
    1
    Views:
    439
    Brian D
    Dec 24, 2009
  5. Brian D
    Replies:
    1
    Views:
    422
    Brian D
    Dec 24, 2009
Loading...

Share This Page