Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #12057

Re: Help on instance of closeable_response in module Mechanize

References <8199DBA0-5A00-4932-9B7C-A5BAD8EC6EEA@mssm.edu>
Date 2011-08-22 17:27 -0700
Subject Re: Help on instance of closeable_response in module Mechanize
From Chris Rebert <crebert@ucsd.edu>
Newsgroups comp.lang.python
Message-ID <mailman.334.1314059312.27778.python-list@python.org> (permalink)

Show all headers | View raw


On Mon, Aug 22, 2011 at 5:17 PM, Yingjie Lin <Yingjie.Lin@mssm.edu> wrote:
> Hi Python users,
>
> I have a question about the instance of closeable_response in module Mechanize.
>
>        from mechanize import ParseResponse, urlopen
>        url = "http://wwwsearch.sourceforge.net/mechanize/example.html"
>        r = urlopen(url)
>        forms = ParseResponse(r, backwards_compat=False)
>        html_lines = r.read()
>
> If I call ParseResponse() before r.read(), then lforms would be a list containing one form
> instance, and html_lines would be an empty string. If I call r.read() first, then html_lines
> would be the HTML source code of the page, but forms would be an empty list.
>
> Therefore, I have to open the url twice, once for each function, like this:
>
>        r = urlopen(url)
>        forms = ParseResponse(r, backwards_compat=False)
>        r = urlopen(url)
>        html_lines = r.read()
>
> I believe this shouldn't be necessary. What is the proper way of doing it? Thank you.

Untested speculation:

from StringIO import StringIO
r = urlopen(url)
html = r.read()
s = StringIO(html)
forms = ParseResponse(s, backwards_compat=False)

Cheers,
Chris

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Help on instance of closeable_response in module Mechanize Chris Rebert <crebert@ucsd.edu> - 2011-08-22 17:27 -0700

csiph-web