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


Groups > comp.lang.python > #12057 > unrolled thread

Re: Help on instance of closeable_response in module Mechanize

Started byChris Rebert <crebert@ucsd.edu>
First post2011-08-22 17:27 -0700
Last post2011-08-22 17:27 -0700
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

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

#12057 — Re: Help on instance of closeable_response in module Mechanize

FromChris Rebert <crebert@ucsd.edu>
Date2011-08-22 17:27 -0700
SubjectRe: Help on instance of closeable_response in module Mechanize
Message-ID<mailman.334.1314059312.27778.python-list@python.org>
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

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web